2017-01-20 49 views
0

我能截取我的活動並存儲文件 但是當我嘗試分享它時,它給我無法加載附件。 我是android新手。任何幫助都是有用的。Android:無法加載附件錯誤?

這是我的代碼:

public Bitmap takeScreenshot() { 
    View rootView = getWindow().getDecorView().findViewById(android.R.id.content).getRootView(); 
    rootView.setDrawingCacheEnabled(true); 
    Bitmap bitmap = Bitmap.createBitmap(rootView.getDrawingCache()); 
    rootView.setDrawingCacheEnabled(false); 
    Log.i("Screenshot","TAKEN"); 
    return bitmap; 

} 

public void saveBitmap(Bitmap bitmap) { 
    imagePath = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/screenshot.png"); 
    FileOutputStream fos; 
    try { 
     fos = new FileOutputStream(imagePath); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
     Log.i("Screenshot","SAVED at"+imagePath); 
     fos.flush(); 
     fos.close(); 
    } catch (IOException e) { 
     Log.e("GREC", e.getMessage(), e); 
    } 
} 

private void shareIt() { 
    Uri uri = Uri.fromFile(imagePath); 
    Intent sharingIntent = new Intent(Intent.ACTION_SEND); 
    sharingIntent.setType("image/*"); 
    String shareBody = "My highest score is "; 
    sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "My score"); 
    sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody); 
    sharingIntent.putExtra(Intent.EXTRA_STREAM, uri); 
    Log.i("IMAGE SHARING","TRY..."); 

    try { 
     startActivity(Intent.createChooser(sharingIntent, "Share via")); 
    }catch(Exception e){ 
     Log.i("try","failed"); 
     Toast.makeText(getApplicationContext(), "No App Available", Toast.LENGTH_SHORT).show(); 
    } 
} 

它是不是給我一個錯誤,但是當我嘗試共享它,它給了我「無法加載附件」。我哪裏錯了?

LOG

01-20 21:10:53.689 3553-3582/com.quickyy.guess.com.quickyy W/OpenGLRenderer:無法設置EGL_SWAP_BEHAVIOR上表面0xac3d5c60,誤差= EGL_BAD_MATCH

01 -20 21:10:53.886 3553-3582/com.quickyy.guess.com.quickyy E/Surface:getSlotFromBufferLocked:unknown buffer:0xb40974e0

01-20 21:10:55.259 3553-3553/com.quickyy。 guess.com.quickyy I /屏幕截圖:TAKEN

01-20 21:10:55.340 3553-3553/com。 quickyy.guess.com.quickyy I/Screenshot:SAVED at/storage/emulated/0/screenshot.png

01-20 21:10:55.506 3553-3553/com.quickyy.guess.com.quickyy I /圖像共享:TRY ...

01-20 21:10:58.858 3553-3582/com.quickyy.guess.com.quickyy E /表面:getSlotFromBufferLocked:未知緩衝區:0xb4097a90

01-20 21 :11:03.767 3553-3582/com.quickyy.guess.com.quickyy E/EGL_emulation:tid 3582:eglSurfaceAttrib(1165):error 0x3009(EGL_BAD_MATCH)

01-20 21:11:03.767 3553-3582/w/OpenGLRenderer:Fa.quickyy.guess.com.quickyy:失敗在表面0xac3d5c60上設置EGL_SWAP_BEHAVIOR,錯誤= EGL_BAD_MATCH

+0

替換'圖像/ *''與圖像/ jpeg'。將'saveBitmap()'移動到後臺線程,並在'fos.flush()'和'fos.close()'之間添加'fos.getFD()。sync()'。在Android 7.0+上,一旦你的'targetSdkVersion'達到24或更高,你將不再能夠使用'Uri.fromFile()',並且需要使用'FileProvider'或者等價物。另請注意,'ACTION_SEND'實現不需要在同一個'Intent'上同時支持'EXTRA_TEXT'和'EXTRA_STREAM'。 – CommonsWare

+0

添加一個logcat,以便我們可以更多地瞭解發生了什麼 –

+0

我假設你的意思是'Log.i(「try」,「failed」);',返回_failed_?你能改變這個日誌行爲 - 'Log.e(「try」,「failed」,e);';這會將異常轉儲到LogCat。然後請編輯問題以包含此輸出。 –

回答

0

您是否設置了媒體存儲的權限?

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 

而在代碼,如果機器人> 5:

private static final int REQUEST_MEDIA = 1; 
private static String[] PERMISSIONS_STORAGE = { 
     Manifest.permission.READ_EXTERNAL_STORAGE, 
     Manifest.permission.WRITE_EXTERNAL_STORAGE, 
}; 

int permissionMedia = ActivityCompat.checkSelfPermission(PSImageBig.this, Manifest.permission.WRITE_EXTERNAL_STORAGE); 
    if (permissionMedia != PackageManager.PERMISSION_GRANTED) { 
     ActivityCompat.requestPermissions(PSImageBig.this, PERMISSIONS_STORAGE, REQUEST_MEDIA); 
    }else{ 
     //code to take screenshot 
    } 

    @Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
    Log.i("", "handleSignInResult onRequestPermissionsResult:" + requestCode); 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    if(requestCode == 1){ 
     if (grantResults.length > 0 
       && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
         //code to take screenshot 
      } 
     } 
    } 
} 
+0

是的,我已經使用權限,也檢查相同 – user3762660