活动返回图像 [英] Activity return a image

查看:141
本文介绍了活动返回图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想回到一个位图在我的活动,以便其他应用程序可以使用它。

返回的文本是明确的。

意图数据=新的意图(); data.putExtra(text1中的文字); data.putExtra(文本2,长文); 的setResult(RESULT_OK,数据);

但如何返回一个位图?

更多信息: 该活动有几个意图,以供大家谁想要得到一个图像。

 <意向滤光器>
    <作用机器人:名称=android.intent.action.GET_CONTENT/>
    <类机器人:名称=android.intent.category.OPENABLE/>
    <类机器人:名称=android.intent.category.DEFAULT/>
    <数据机器人:MIMETYPE =图像/ */>
    <数据机器人:MIMETYPE =vnd.android.cursor.dir /图片/>
&所述; /意图滤光器>
<意向滤光器>
    <作用机器人:名称=android.intent.action.PICK/>
    <类机器人:名称=android.intent.category.DEFAULT/>
    <数据机器人:MIMETYPE =图像/ */>
    <数据机器人:MIMETYPE =vnd.android.cursor.dir /图片/>
&所述; /意图滤光器>
 

编辑: 这是一个功能的解决方案:

公共无效结束(位图位图){     尝试 {         文件夹=新的文件(Environment.getExternalStorageDirectory()+/图标选择/);         如果(!folder.exists()){             folder.mkdirs();         }         文件nomediaFile =新的文件(文件夹,.nomedia);         如果(!nomediaFile.exists()){             nomediaFile.createNewFile();         }         FileOutputStream中出=新的FileOutputStream(Environment.getExternalStorageDirectory()+/图标选择/ latest.png);         bitmap.com preSS(Bitmap.Com pressFormat.PNG,90,出);         文件bitmapFile =新的文件(Environment.getExternalStorageDirectory()+/图标选择/ latest.png);         如果(bitmapFile.exists()){             。意图localIntent =新意图()使用setData(Uri.fromFile(bitmapFile));             的setResult(RESULT_OK,localIntent);         } 其他 {             的setResult(RESULT_CANCELED);         }         super.finish();     }赶上(例外五){         e.printStackTrace();         Log.d(beewhale,错误写入数据);     } }

解决方案

虽然我同意奥维迪乌Latcu ,你可能会遇到内存问题。

这可能是更好的位图存储在SD卡一个临时位置。然后从那里访问它。 (写和读的位图文件)。

另外,如果你想要去把一个字节数组作为一个额外的,COM preSS位图到另一种格式的第一,如JPEG或类似的,这将再次减少内存问题的变化。

一个简单的800万像素的图像,为32MB(4层)。这超过了大多数(如果不是全部)手机允许的内存使用情况的应用程序。

直接存储到存储是如何在内置摄像头的应用工程,以避免这个问题。

下面是我的$ C $下处理它:

 公共无效showCameraScreen(查看视图){
    //内置摄像头
    意图摄像头=新的意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
    camera.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(getTempFile(本)));
    this.startActivityForResult(摄像机,1);

}

私人文件getTempFile(上下文的背景下){
    //它将返回/sdcard/MyImage.tmp
    最终的文件路径=新的文件(Environment.getExternalStorageDirectory(),context.getPackageName());
    如果(!path.exists()){
        path.mkdir();
    }
    返回新的文件(路径,MyImage.tmp);
}

@覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
    如果(要求code == 1安培;&安培;结果code == RESULT_OK){
        最终文件的文件= getTempFile(本);

        字节[] _data =新的字节[(INT)file.length()];
        尝试 {
            InputStream的时间=新的FileInputStream(文件);
            in.read(_data);
            附寄();
            在= NULL;
            //做你想要的_data。图像的字节数组。
        }赶上(例外五){

        }
    }
}
 

请注意:您还需要编写code对你的所谓的意图,其存储传入乌里 - 在这里,内置的摄像头API做它

I want to return a bitmap in my activity, so other applications can use it.

Returning a text is clear.

Intent data = new Intent();
data.putExtra("text1", "text.");
data.putExtra("text2", "longer text.");
setResult(RESULT_OK, data);

But how to return a bitmap?

More Information: The activity has several intents to be available for everyone who wants to get a image.

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

EDIT: Here is the solution in one function:

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

解决方案

While I agree with Ovidiu Latcu, you may run into memory issues.

It may be better to store the Bitmap to a temp location on the SD Card. And then access it from there. (Write and Read the Bitmap to file).

Alternatively, if you do want to go with putting a byte array as an extra, Compress the bitmap to another format first, e.g. Jpeg or similar, this again will reduce changes of memory issues.

A simple 8M pixel image, is 32MB (4 layers). And this exceeds the allowed memory usage on most (if not all) phones for an application.

Storing straight to the Storage is how the built in camera app works to avoid this problem.

Here is my code for dealing with it:

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

        }
    }
}  

Note: You would also need to write code on your called intent, that stores to the passed Uri - Here, the built in camera api does it.

这篇关于活动返回图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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