2017-01-20 51 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