【问题标题】:Sharing images that are stored on internal memory共享存储在内存中的图像
【发布时间】:2012-12-13 21:20:19
【问题描述】:

我有一个应用程序,其中设置了 ImageView,可以单击以在图库中打开。

默认情况下,我使用以下代码从外部存储中获取文件目录来存储我的 jpeg:

File picsDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"MyCameraApp");

但是!假设外部存储未安装或根本不存在(Galaxy Nexus),这不起作用。所以我围绕它写了一个 if 语句,并获取内部缓存目录作为后备。

String state = Environment.getExternalStorageState()
if(Environment.MEDIA_MOUNTED.equals(state)){ 
    File picsDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"MyCameraApp");    
}else{
    context.getCacheDir();
}

图像在 ImageView 中显示良好,但在我的意图启动时不显示。

Intent intent = new Intent();             
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(imgFile), "image/jpeg");
startActivity(intent);

图库已加载,但显示黑屏。大概是因为画廊无法访问我的应用程序缓存目录中的文件。

作为替代方案,我尝试使用使用MediaStore.Images.Media.INTERNAL_CONTENT_URI 的媒体内容提供程序,但这会在尝试插入图像时导致错误:

java.lang.UnsupportedOperationException: Writing to internal storage is not supported.

我该怎么办?

【问题讨论】:

  • 也许我可以尝试将位图与意图一起打包?喜欢intent.putExtra("BitmapImage", bitmap);。我的图片通常约为 5MB。
  • 您可以将图像存储在应用程序的私有存储中,并授予对这些图像的一般只读访问权限,但是包括一些 google 内置程序在内的许多应用程序在提交时不会费心检查它们是否可以访问这些图像到它们的路径,但会简单地假设它们不能并且失败,即使拥有的应用程序实际上已授予全局读取访问权限。

标签: android storage


【解决方案1】:

我想这里的问题是您正在尝试使用画廊打开一个保存在私有内存空间中的文件(getCacheDir 返回相对于您的应用程序的路径,并且只有您的应用程序可以访问该内存路径)

如果您无法保存在外部存储器中,您可以尝试保存在公共路径中(但这样您的媒体文件可以被每个应用程序操作,如果您卸载应用程序,它不会清除您生成的媒体文件保存在那里)

如果你想使用私有内存,你可以写你的 ContentProvider

我编辑发布我用来完成我所说的内容提供者。 这是我的内容提供商(我刚刚发布了您需要的相关部分):

public class MediaContentProvider extends ContentProvider {
private static final String TAG = "MediaContentProvider";

// name for the provider class
public static final String AUTHORITY = "com.way.srl.HandyWay.contentproviders.media";

private MediaData _mediaData;

// UriMatcher used to match against incoming requests
private UriMatcher _uriMatcher;

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public String getType(Uri uri) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public boolean onCreate() {
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    // Add a URI to the matcher which will match against the form
    // 'content://com.stephendnicholas.gmailattach.provider/*'
    // and return 1 in the case that the incoming Uri matches this pattern
    _uriMatcher.addURI(AUTHORITY, "*", 1);

    return true;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {

    Log.v(TAG, "Called with uri: '" + uri + "'." + uri.getLastPathSegment());

    // Check incoming Uri against the matcher
    switch (_uriMatcher.match(uri)) {

    // If it returns 1 - then it matches the Uri defined in onCreate
        case 1:

            // The desired file name is specified by the last segment of the
            // path
            // E.g.
            // 'content://com.stephendnicholas.gmailattach.provider/Test.txt'
            // Take this and build the path to the file
            // String fileLocation = getContext().getCacheDir() + File.separator + uri.getLastPathSegment();
            Integer mediaID = Integer.valueOf(uri.getLastPathSegment());

            if (_mediaData == null) {
                _mediaData = new MediaData();
            }
            Media m = _mediaData.get(mediaID);

            // Create & return a ParcelFileDescriptor pointing to the file
            // Note: I don't care what mode they ask for - they're only getting
            // read only
            ParcelFileDescriptor pfd = ParcelFileDescriptor.open(new File(m.filePath), ParcelFileDescriptor.MODE_READ_ONLY);
            return pfd;

            // Otherwise unrecognised Uri
        default:
            Log.v(TAG, "Unsupported uri: '" + uri + "'.");
            throw new FileNotFoundException("Unsupported uri: " + uri.toString());
    }
}

那么你需要在清单中引用你的内容提供者,在我的例子中是

<provider
        android:name=".contentproviders.MediaContentProvider"
        android:authorities="com.way.srl.HandyWay.contentproviders.media" >
    </provider>

然后像这样使用它

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("content://" + MediaContentProvider.AUTHORITY + "/" + m.id), "image/jpg");

在我的情况下,m 是一个实体,它存储一个指向 sqlite 数据库的 id,我使用一个获取数据的类来再次填充对象(使用 _mediaData),您可以更改代码以满足您的需求

这样我在我的应用程序中完全解决了你的问题

【讨论】:

【解决方案2】:

我知道不需要这种后备。具有 Google Play 的设备保证在Environment.getExternalStorageDirectory() 中至少有 2 GB 可用空间。

我想在 Galaxy Nexus 中,这是标记为外部的内部存储器上的一个分区。如果它不可用,我只会显示警告。

【讨论】:

  • 有点。 Galaxy Nexus 绝对有一个“外部存储”,即使它是内部的且不可移动的。但是,在已移除存储的设备上,它可能不存在;在进行 USB 大容量存储的设备上,它在安装到外部计算机时可能不可用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-03
  • 1970-01-01
  • 2017-02-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多