0

我想分享存储在内部存储中的屏幕截图,但我在分享时得到的只是一个空白屏幕。我开始知道另一个应用程序不能使用那些赢得应用程序私有数据的应用程序。这是我用来保存图像的代码:

File path = new File(getApplicationContext().getDir("Default", Context.MODE_ENABLE_WRITE_AHEAD_LOGGING),"myapp");
File directory = new File(path.getAbsolutePath());
directory.mkdirs();
String filename = "myapp.png";
File yourFile = new File(directory, filename);
try
{
 FileOutputStream out = new FileOutputStream(yourFile, true);
 bitmap.compress(Bitmap.CompressFormat.PNG, 90,out);
 out.flush();
 out.close();
 send(yourFile);
}catch (IOException e)
{
 e.printStackTrace();
}

分享意图是:

public void send(File path)
{
    Uri uri = Uri.fromFile(path);
    Log.d("uri", String.valueOf(path));
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, "My App");
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.setType("image/*");
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(Intent.createChooser(shareIntent, "Share via"));
}

保存图像的路径和路径的值为:

/data/user/0/com.sample.myapp/app_Default/myapp/myapp.png

现在我将如何确保屏幕截图被访问并可供共享。

4

2 回答 2

0

您获得的图像 URI 不是真实图像路径

获得真实路径检查这个

public String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null); 
    cursor.moveToFirst(); 
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
    return cursor.getString(idx); 
}

只需传递此图像 uri 并获取完整的图像路径

于 2016-06-30T10:18:42.490 回答
0

我通过将图像导出到外部存储然后共享来解决它。

 String url = null;
    try {
        url = MediaStore.Images.Media.insertImage(getContentResolver(), path.getAbsolutePath(), path.getName(), path.getName());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    Uri uri = Uri.parse(url);
于 2016-06-30T13:54:50.997 回答