2013-05-17 11 views
8

Sto scattando istantanee e creato miniature e poi condivido questa immagine. Ma la miniatura mostra tutto nero. Ho usato il seguente codiceLa schermata mostra nero

Bitmap bitmap; 
View v1 = v.getRootView(); 
v1.setDrawingCacheEnabled(true); 
bitmap = Bitmap.createBitmap(v1.getDrawingCache()); 
String url = Images.Media.insertImage(
mContext.getContentResolver(), bitmap, "title", null); 

Qualcuno può dirmi cosa c'è di sbagliato in questo codice.

EDIT

private View.OnClickListener shareListener = new View.OnClickListener() { 
     public void onClick(View v) { 
      Bitmap bitmap; 
      View v1 = v.getRootView(); 
      v1.setDrawingCacheEnabled(true); 
      bitmap = Bitmap.createBitmap(v1.getDrawingCache()); 
      String url = Images.Media.insertImage(
        mContext.getContentResolver(), bitmap, "title", null); 
      v1.setDrawingCacheEnabled(false); 
      Activity activity = (Activity) getContext(); 
      Intent share = new Intent(Intent.ACTION_SEND); 
      share.setType("image/jpeg"); 
      share.putExtra(Intent.EXTRA_STREAM, Uri.parse(url)); 
      activity.startActivity(Intent.createChooser(share, 
        "Share")); 

     } 

    }; 

nero Immagine enter image description here

+0

mettere un po 'più di codice ... – Riser

+0

Questo è il codice che sto utilizzando per la cattura screenshot – Dilip

+0

Hai controllato questo? http://stackoverflow.com/questions/16489086/take-screensot-and-save-android –

risposta

3

Usa seguente codice che può funzionare per voi. Grazie

SCREENSHOTS_LOCATIONS = Environment.getExternalStorageDirectory().toString() + "/screenshots/"; 
// Get root view 
View view = activity.getWindow().getDecorView().getRootView(); 
// Create the bitmap to use to draw the screenshot 
final Bitmap bitmap = Bitmap.createBitmap(screenWidth, screenHeight, Bitmap.Config.ARGB_8888); 
final Canvas canvas = new Canvas(bitmap); 

// Get current theme to know which background to use 
final Theme theme = activity.getTheme(); 
final TypedArray ta = theme 
    .obtainStyledAttributes(new int[] { android.R.attr.windowBackground }); 
final int res = ta.getResourceId(0, 0); 
final Drawable background = activity.getResources().getDrawable(res); 

// Draw background 
background.draw(canvas); 

// Draw views 
view.draw(canvas); 

// Save the screenshot to the file system 
FileOutputStream fos = null; 
try { 
    final File sddir = new File(SCREENSHOTS_LOCATIONS); 
    if (!sddir.exists()) { 
     sddir.mkdirs(); 
    } 
    fos = new FileOutputStream(SCREENSHOTS_LOCATIONS 
      + System.currentTimeMillis() + ".jpg"); 
    if (fos != null) { 
     if (!bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos)) { 
      Log.d("ScreenShot", "Compress/Write failed"); 
     } 
     fos.flush(); 
     fos.close(); 
    } 

} catch (FileNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
+2

cosa c'è di sbagliato nel codice pubblicato nella domanda? – Raghunandan

+0

Ottenere la radice La vista è qui fatta come quello che ho scritto nel mio codice. La seconda cosa per salvare il file imag è meglio fare ciò che ho scritto in questo codice. –

+1

puoi dirmi cosa c'è di sbagliato con il codice Grazie – Dilip

0

provare

v1.setDrawingCacheEnabled(true); 

// this is the important code :) 
// Without it the view will have a dimension of 0,0 and the bitmap will be null   
v1.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
      MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 
v1.layout(0, 0, v1.getMeasuredWidth(), v1.getMeasuredHeight()); 
//Forces the drawing cache to be built if the drawing cache is invalid. 
v1.buildDrawingCache(true); 
Bitmap b = Bitmap.createBitmap(v1.getDrawingCache()); 
v1.setDrawingCacheEnabled(false); // clear drawing cache 
0

provate ad applicare questo codice:

private File takeScreenshot(boolean showToast) { 
    View v = getWindow().getDecorView(); 

    v.setDrawingCacheEnabled(true); 
    Bitmap cachedBitmap = v.getDrawingCache(); 
    Bitmap copyBitmap = cachedBitmap.copy(Bitmap.Config.RGB_565, true); 
    FileOutputStream output = null; 
    File file = null; 
    try { 
     File path = Places.getScreenshotFolder(); 
     Calendar cal = Calendar.getInstance(); 

     file = new File(path, 

     cal.get(Calendar.YEAR) + "_" + (1 + cal.get(Calendar.MONTH)) + "_" 
       + cal.get(Calendar.DAY_OF_MONTH) + "_" 
       + cal.get(Calendar.HOUR_OF_DAY) + "_" 
       + cal.get(Calendar.MINUTE) + "_" + cal.get(Calendar.SECOND) 
       + ".png"); 
     output = new FileOutputStream(file); 
     copyBitmap.compress(CompressFormat.PNG, 100, output); 
    } catch (FileNotFoundException e) { 
     file = null; 
     e.printStackTrace(); 
    } finally { 
     if (output != null) { 
      try { 
       output.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

    } 

    if (file != null) { 
     if (showToast) 
      Toast.makeText(getApplicationContext(), 
        "Saved " + file.getAbsolutePath(), 
        Toast.LENGTH_LONG).show(); 
     // sending a broadcast to the media scanner so it will scan the new 
     // screenshot. 
     Intent requestScan = new Intent(
       Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
     requestScan.setData(Uri.fromFile(file)); 
     sendBroadcast(requestScan); 

     return file; 
    } else { 
     return null; 
    } 
} 

Questo funziona perfettamente per me. Spero che questo ti possa aiutare.

-1

provare questo codice .. Dovrebbe essere lavoro.

linearLayout.setDrawingCacheEnabled(true); 
    linearLayout.measure(MeasureSpec.makeMeasureSpec(linearLayout.getWidth(), MeasureSpec.AT_MOST),MeasureSpec.makeMeasureSpec(linearLayout.getHeight(), MeasureSpec.EXACTLY)); 
    linearLayout.layout(0, 0, linearLayout.getMeasuredWidth(), linearLayout.getMeasuredHeight()); 

    Bitmap b1 =linearLayout.getDrawingCache(); 


    Bitmap bigbitmap = Bitmap.createBitmap(linearLayout.getMeasuredWidth(), linearLayout.getMeasuredHeight(), Bitmap.Config.ARGB_4444); 
    Canvas bigcanvas = new Canvas(bigbitmap); 

    Paint paint = new Paint(); 
    bigcanvas.drawBitmap(b1, 0, 0, paint); 


    int Measuredwidth = 0; 
    int Measuredheight = 0; 
    Point size = new Point(); 
    WindowManager w = getWindowManager(); 

     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2){ 
      w.getDefaultDisplay().getSize(size); 

      Measuredwidth = size.x; 
      Measuredheight = size.y; 
      }else{ 
      Display d = w.getDefaultDisplay(); 
      Measuredwidth = d.getWidth(); 
      Measuredheight = d.getHeight(); 
      } 
     Log.e(DEB_TAG, ""+Measuredwidth); 
     Log.e(DEB_TAG, ""+Measuredheight); 
    bigbitmap = Bitmap.createScaledBitmap(bigbitmap, Measuredwidth, Measuredheight, true); 

    visiterCoverFlow.setScreenCache(bigbitmap); 
Problemi correlati