【问题标题】:Large image Extra in Intent, cause black Screen in AndroidIntent中的大图像额外,导致Android中的黑屏
【发布时间】:2012-07-29 16:54:24
【问题描述】:

我有这个代码可以从画廊或相机中挑选图像:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case SELECT_PICTURE_ACTIVITY_REQUEST_CODE:
            if (resultCode == RESULT_OK) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA,
                        MediaStore.Images.Media.DISPLAY_NAME };
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                if (cursor.moveToFirst()) {
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String filePath = cursor.getString(columnIndex);
                    ext = filePath
                            .substring(filePath.lastIndexOf(".") + 1);
                    photod = BitmapFactory.decodeFile(filePath);

                    new AsyncTaskOne().execute(new String[] {});
    (fileNameIndex);


                }
            }
            break;

        case SELECT_CAMERA_ACTIVITY_REQUEST_CODE:
            if (requestCode == CAMERA_REQUEST) {
                photod = (Bitmap) data.getExtras().get("data");
                new AsyncTaskOne().execute(new String[] {});
            }
        }
    }

当我按下确认按钮时,我会调用这个监听器

OnClickListener confirm = new OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent("com.striget.eu.UpdatePhoto");
            i.putExtra(PropertiesUpdatedPhoto.EXTRA_PHOTO, codedPhoto);
            i.putExtra(PropertiesUpdatedPhoto.EXTRA_EXT, ext);
            startActivity(i);
        }
    };

(其中 codedPhoto 是通过另一种方法以 base64 编码的图像) 在另一个活动中发送图像及其扩展

小图像一切正常,但如果我选择中等大小的图像或大照片(如果不是很大),应用程序会冻结,屏幕变黑,如果我等待一些分钟返回当前活动而不在堆栈中显示任何错误,并且调用的意图 PropertiesUpdatedPhoto 没有启动。

我该如何解决这个问题?

【问题讨论】:

    标签: android android-intent android-asynctask android-imageview


    【解决方案1】:

    这个问题是讨论here,我们应该尽量减少intent extra content。

    我的建议是传递图像而不是传递图像,也许传递图像 URI 或路径会是更好的解决方案。然后只在该活动中加载图像。

    例子:

    OnClickListener confirm = new OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent("com.striget.eu.UpdatePhoto");
            i.setData(PropertiesUpdatedPhoto.EXTRA_PHOTO, # Image URI Here # );
            // Or i.putExtra(PropertiesUpdatePhoto.EXTRA_PHOTO, # Image Path Here #);
            i.putExtra(PropertiesUpdatedPhoto.EXTRA_EXT, ext);
            startActivity(i);
        }
    };
    

    【讨论】:

    • 我必须传递给其他活动的图像需要在当前活动中编码。那么:如何将编码图像存储在临时文件中以仅传递路径?
    • 是的,正如您所建议的将编码图像存储为临时文件并获取临时文件路径并将其传递给新活动。只是好奇为什么需要对图像进行编码?
    • 我必须将图像嵌套在 JSON 中才能将其发送到 Web 服务。
    • 好的,我的建议是先解码图像,然后再将它们存储到图像中。如果解码后的数据是字符串格式。你可以这样做 InputStream is = new ByteArrayInputStream(str.getBytes());其余的,您可以在此处按照此代码 sn-p stackoverflow.com/a/3296950/1559049
    • 你需要在你的 AndroidManifest.xml 中包含
    猜你喜欢
    • 2013-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    相关资源
    最近更新 更多