2014-05-21 139 views
0

我創建了一個截屏截圖的功能,將其保存在類型爲.jpeg的臨時文件中,然後允許用戶在Facebook或藍牙上共享它。這裏是我的共享功能:在Android上分享屏幕截圖。每次共享相同的圖像

public Bitmap Share(View v) { 
    // Sound 
    soundPool.play(button_sound, 1.0f, 1.0f, 0, 0, 1.0f); 

    // Image 
    v.setDrawingCacheEnabled(true); 
    v.setLayerType(View.LAYER_TYPE_NONE, null); 
    Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache()); 
    File file = new File(Environment.getExternalStorageDirectory() 
      + File.separator + "temporary_file.jpg"); 
    try { 
     file.createNewFile(); 
     FileOutputStream ostream = new FileOutputStream(file); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 90, ostream); 
     ostream.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    // Share 
    Intent share = new Intent(Intent.ACTION_SEND); 
    share.setType("image/jpeg"); 
    String filel = "file://" + Environment.getExternalStorageDirectory() 
      + File.separator + "temporary_file.jpg"; 
    share.putExtra(Intent.EXTRA_STREAM, Uri.parse(filel)); 

    startActivity(Intent.createChooser(share, "Share Image")); 
    return bitmap; 
} 

我的問題是,它需要的截圖,但隨後過來,當我嘗試一種全新的分享一個超過總是共享相同的屏幕截圖。當我使用文件管理器檢查時,圖像是不同的。所以我不知道是什麼原因造成的。

非常感謝你的時間。

+0

你有沒有注意到新創建的文件的名稱與舊的名稱相同,爲什麼不生成隨機文件名? –

回答

1

首先,當文件不存在file.createNewFile()纔有效。失敗時,它不會引發任何異常,它只是成功返回truefalse的失敗,

同樣是bitmap.compress(Bitmap.CompressFormat.JPEG, 90, ostream);如此。這種方法在失敗時也不會拋出任何異常,並且只返回true取得成功,false失敗,

我不知道它是否會導致錯誤,但您可能想查看該錯誤。例如,您可以嘗試在文件已存在時首先刪除該文件。

1

前請getBitmap

無效()你的看法是這樣的:

v.inValidated(); 
v.setDrawingCacheEnabled(true);