Android的MediaStore不能被插入的图像数据 [英] Android MediaStore can't get inserted image data

查看:661
本文介绍了Android的MediaStore不能被插入的图像数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有图像导出函数图像编辑应用程序。 previously我trye​​d直接通过传递文件路径URI来ACTION_SEND意图导出。是这样的:

I have image editing app with image export function. Previously I tryed to export it directly by passing file path uri to ACTION_SEND intent. Something like:

Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                shareIntent.setType("image/*");
                shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                startActivity(Intent.createChooser(shareIntent, getString(R.string.export)));

但是,并非所有的应用程序能正确解析这样的意图。所以,现在我通过添加图像Android的MediaStore第一prepare URI:

But not all apps can correctly parse such intent. So now I first prepare uri by adding image to android's MediaStore:

shareUri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(),
                            file.getAbsolutePath(), file.getName(), projectName));

...然后传递到shareUri意图。当用户导出图像的副本,添加到Android MediaStore(即使图像路径和名称相同)每次 - 现在,所有的应用程序能够正确处理这个意图,但有一个问题出现。

...and then passing shareUri to intent. Now all apps can correctly process this intent, but one problem appear - each time when user exports image its copy added to android MediaStore(even if image path and name the same).

我试图确定已经添加图像按照code到mediastore:

I trying to determine is image already added to mediastore by following code:

Cursor c = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Images.Media.DATA + "=?",
                    new String[] { filePath }, null);
if (c.getCount() > 0 && c.moveToFirst()) {
  shareUri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        "" + c.getInt(c.getColumnIndex(MediaStore.Images.Media._ID)));
                c.close();
} else {
  shareUri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(),
                            file.getAbsolutePath(), file.getName(), projectName));
}

...但c.getCount()总是== 0,即使我从MediaStore.Images.Media.insertImage复制保存的URI和

...but c.getCount() always == 0. Even if I copy saved uri from MediaStore.Images.Media.insertImage and query it directly by

Cursor c = getContentResolver().query(savedUri, null, null, null);

我怎么可以检测是图像文件已在MediaStore?

How I can detect is image file already in MediaStore?

推荐答案

找到解决办法:不使用MediaStore.Images.Media.insertImage到文件添加到图库但MediaScannerConnection做类似的:

Found solution: not using MediaStore.Images.Media.insertImage to add file to gallery but doing similar by MediaScannerConnection:

MediaScannerConnection.scanFile(
                      getApplicationContext(), 
                      new String[]{file.getAbsolutePath()}, 
                      new String[]{"image/*"}, 
                      new OnScanCompletedListener() {
                         @Override
                         public void onScanCompleted(String path, Uri uri) {
                             Intent shareIntent = new Intent();
                            shareIntent.setAction(Intent.ACTION_SEND);
                            shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
                            shareIntent.setType("image/*");
                            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                            startActivity(Intent.createChooser(shareIntent, getString(R.string.export)));
                         }
                      });

这篇关于Android的MediaStore不能被插入的图像数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆