【问题标题】:Share screenshot from internal storage android从内部存储android分享屏幕截图
【发布时间】:2016-06-30 10:12:56
【问题描述】:

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

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"));
}

图片保存的路径,path的值为:

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

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

【问题讨论】:

    标签: android share android-internal-storage


    【解决方案1】:

    你得到的图片 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 并获取完整的图像路径

    【讨论】:

    • 我已经实现了你的代码并在上面编辑了我的代码。现在,当我运行代码时,应用程序崩溃了。我哪里错了?
    • 什么是异常?
    • 这就是我得到的“java.lang.NullPointerException:尝试在空对象引用上调用接口方法'boolean android.database.Cursor.moveToFirst()'”
    【解决方案2】:

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

     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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多