活动返回一张图片

8
我希望在我的活动中返回一个位图,以便其他应用程序可以使用它。
返回文本是清晰的。
Intent data = new Intent();
data.putExtra("text1", "text.");
data.putExtra("text2", "longer text.");
setResult(RESULT_OK, data);

但如何返回位图呢?

更多信息: 该活动有多个意图,可供每个想要获取图像的人使用。

<intent-filter>
    <action android:name="android.intent.action.GET_CONTENT" />
    <category android:name="android.intent.category.OPENABLE" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="image/*" />
    <data android:mimeType="vnd.android.cursor.dir/image" />
</intent-filter>
<intent-filter>
    <action android:name="android.intent.action.PICK" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="image/*" />
    <data android:mimeType="vnd.android.cursor.dir/image" />
</intent-filter>

编辑: 以下是一种函数解决方案:

public void finish(Bitmap bitmap) {
    try {
        File folder = new File(Environment.getExternalStorageDirectory() + "/Icon Select/");
        if(!folder.exists()) {
            folder.mkdirs();
        }
        File nomediaFile = new File(folder, ".nomedia");
        if(!nomediaFile.exists()) {
            nomediaFile.createNewFile();
        }

        FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/Icon Select/latest.png");
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
        File bitmapFile = new File(Environment.getExternalStorageDirectory() + "/Icon Select/latest.png");

        if(bitmapFile.exists()) {
            Intent localIntent = new Intent().setData(Uri.fromFile(bitmapFile));
            setResult(RESULT_OK, localIntent);                
        } else {
            setResult(RESULT_CANCELED);
        }
        super.finish();

    } catch (Exception e) {
        e.printStackTrace();
        Log.d("beewhale", "Error writing data");
    }
}

你想要实现什么目标?请描述一下。 - Krishna Suthar
2个回答

5

我同意Ovidiu Latcu的看法,但是你可能会遇到内存问题。

更好的方法是将Bitmap存储到SD卡上的临时位置,并从那里访问它。(将Bitmap写入文件并读取)

或者,如果您确实想使用字节数组作为额外的内容,请先将Bitmap压缩为另一种格式,例如Jpeg或相似格式,这样可以减少内存问题的发生。

一个简单的8M像素图像为32MB(4层)。这超出了大多数手机应用程序允许的内存使用。

内置相机应用程序通过直接存储到存储空间来避免这个问题。

以下是处理此问题的代码:

public void showCameraScreen(View view) {
    // BUILT IN CAMERA 
    Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);   
    camera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(this)) );   
    this.startActivityForResult(camera, 1);

}   

private File getTempFile(Context context) {
    // it will return /sdcard/MyImage.tmp
    final File path = new File(Environment.getExternalStorageDirectory(), context.getPackageName());
    if (!path.exists()) {
        path.mkdir();
    }
    return new File(path, "MyImage.tmp");
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == RESULT_OK) {
        final File file = getTempFile(this);

        byte[] _data = new byte[(int) file.length()];
        try {
            InputStream in = new FileInputStream(file);
            in.read(_data);
            in.close();
            in = null;
            //DO WHAT YOU WANT WITH _data. The byte array of your image.
        } catch (Exception E) {

        }
    }
}  

注意:您还需要在调用的意图上编写代码,以将其存储到传递的Uri中 - 在此处,内置的相机API会执行此操作。

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接