首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在两个应用程序之间传递图像

在两个应用程序之间传递图像
EN

Stack Overflow用户
提问于 2018-05-07 11:55:26
回答 4查看 336关注 0票数 2

我有两个应用程序,应用程序A和应用程序B。当我按下应用程序A中的一个按钮时,应用程序B就会被调用。在应用程序B,它有相机活动和其他一些图像处理选项,如裁剪,调整亮度和对比度。经过图像处理后,我需要将图像传递给应用程序A,但是,图像不应该保存在sd卡中。

我尝试过的:将图像用于base64字符串

Step1: i使用以下代码调用AppB

代码语言:javascript
复制
Intent i = new 
Intent("com.appb.ImageProcessingActivity");
            startActivityForResult(i, 100);

Step2:应用程序B中的,经过图像处理后,我将图像转换为base64字符串,并使用此代码将其发送回App。(经过图像处理后,图像大小将小于150 be )

代码语言:javascript
复制
Intent data = new Intent();
data.putExtra("outputImage", base64ImageString);
setResult(RESULT_OK, data);
finish();

Step3:应用程序A中的,我将使用以下代码获取图像细节

代码语言:javascript
复制
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==100 && resultCode==RESULT_OK){

        String outputImage = data.getStringExtra("base64ImageString");
        byte[] decodedString = Base64.decode(outputImage, Base64.DEFAULT);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        imageView.setImageBitmap(decodedByte);

    }
}

但是我无法从App接收base64字符串到App,它显示了以下错误(注意:,我可以使用这种方法发送普通字符串)

代码语言:javascript
复制
JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 1058260)

这个答案不被接受

在应用程序B中,将图像保存在sd卡中,将图像路径发送到应用程序B,然后将图像路径转换为应用程序A中的base64,然后删除sd卡图像。

EN

回答 4

Stack Overflow用户

发布于 2018-05-07 12:06:41

首先,它们被称为活动,而不是“应用程序”。

其次,您得到了这个错误,因为图像太大,无法在这样的活动之间传递。如果您仔细查看您的logcat,可能会在"TransactionTooLargeException".的某个地方发现此错误。

为了避免它,并使您的应用程序工作得更快,您应该将图像保存为外部缓存目录中的文件,并将其路径传递到第二个活动,然后加载它。而且,不要担心图像将被临时保存,因为它位于缓存目录中。

票数 0
EN

Stack Overflow用户

发布于 2018-05-07 12:09:55

代码语言:javascript
复制
Bundle extras = new Bundle();
extras.putParcelable("Bitmap", bmp);
intent.putExtras(extras);
startActivity(intent);

寄出申请-

代码语言:javascript
复制
Button sendImage = (Button) findViewById(R.id.button2);
        sendImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(
                        Intent.createChooser(intent, "Select Picture"),
                        SELECT_PICTURE);
            }
        });

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                System.out.println("Image Path : " + selectedImagePath);
                img.setImageURI(selectedImageUri);
                sendImage(selectedImageUri);
            }
        }
    }

    void sendImage(Uri selectedImageUri) {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, selectedImageUri);
        shareIntent.setType("image/*");
        startActivity(Intent.createChooser(shareIntent, "Send Image"));
    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

接收申请

代码语言:javascript
复制
private Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout linearlyt = new LinearLayout(this);

        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();
        TextView tv = new TextView(this);
        linearlyt .addView(tv);
        linearlyt .setOrientation(LinearLayout.VERTICAL);
        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if ("text/plain".equals(type)) {

                tv.setText(intent.getExtras().getString(Intent.EXTRA_TEXT));
                tv.append("\n" + (intent.getExtras().getString("MyKey")));

            } else if (type.startsWith("image/")) {
                if (bitmap != null) {
                    bitmap.recycle();
                }
                ImageView img = new ImageView(this);
                Bundle bundle = intent.getExtras();
                Uri uri = (Uri) bundle.get(Intent.EXTRA_STREAM);
                img.setImageURI(uri);
                linearlyt .addView(img);
        }

        } else {
            // Handle other intents, such as being started from the home screen
        }
        setContentView(linearlyt);
    }
票数 0
EN

Stack Overflow用户

发布于 2018-05-07 12:22:01

从Android 2开始,每个应用程序都有足够的RAM来存储一个摄像头图像,而不足以存储两个摄像头图像。我不认为这是可能的,但是--您可以在清单中尝试android:largeHeap="true",但是OTOH --我也不认为它会起作用。

作为最后的手段,您可以创建一个服务(在您的应用程序中,可能是在一个单独的进程中),并将图像字节发送到或从该服务中发送。但你确定值得这么做吗?问题是,系统可能决定删除堆中有相机图像的两个应用程序中的一个。

或者,您也可以创建一个ContentProvider并将您的图像存储在那里。谷歌给了我像Share an image with a content provider in Android app这样的网页

还有一个非常疯狂的想法:将图像分割成大小的碎片,这些碎片可以通过一个意图传递,并以多种意图传递:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50213652

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档