2

我正在尝试将由画廊或相机选择的 m 图像发送到另一个活动,但似乎没有发生任何事情,我已将位图转换为字符串以通过意图发送它,但似乎从未调用过意图。

请帮助我我做错了什么?

这是我的 onActivityResult() 代码

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            Bitmap thumbnail = null;
            if(requestCode==RESULT_LOAD_IMAGE){
               if(resultCode==RESULT_OK){

                    Uri selectedImageUri = data.getData();
                    String[] filePathColumn = { MediaStore.Images.Media.DATA };
                    Cursor cursor = getContentResolver().query(selectedImageUri,filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String picturePath = cursor.getString(columnIndex);
                    cursor.close();
                    data.getExtras().get("data");

                    thumbnail = (BitmapFactory.decodeFile(picturePath));

                    //CONVERT BITMAP TO STRING
                    ByteArrayOutputStream baos=new  ByteArrayOutputStream();
                    thumbnail.compress(Bitmap.CompressFormat.PNG,100, baos);
                    byte [] b=baos.toByteArray();
                    String temp=Base64.encodeToString(b, Base64.DEFAULT);
                        //trying to start activity...

                    Intent intent = new Intent(Home.this, Upload.class);
                    intent.putExtra("picture", temp);
                    startActivity(intent);}

                }
            }

            }
4

2 回答 2

1

与其将图像转换为字符串,不如使用文件的路径并在其他活动中重新加载它?我的应用程序Pic4Share 遇到了同样的问题,我以这种方式解决了它。

在从图库加载图像的活动中:

String stringPath;

protected void onActivityResult(int requestCode, int resultCode,Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
        switch(requestCode) { 
            case SELECT_PHOTO:
                if(resultCode == RESULT_OK){  
                    Uri selectedImage = imageReturnedIntent.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    stringPath = cursor.getString(columnIndex); // ***Here is stored the path***
                    cursor.close();

                    BitmapFactory.Options opts = new BitmapFactory.Options ();
                    opts.inSampleSize = 4;   // for 1/2 the image to be loaded
                    opts.inPurgeable = true;

                    image.setImageBitmap(decodeSampledBitmapFromFile(txtPath, 800, 800));

                }
                else{
                    //Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

然后,您可以使用意图传递带有路径的字符串:

Intent intent = new Intent(Home.this, Upload.class);
Bundle b = new Bundle();
b.putString("picture", stringPath);
intent.putExtras(b);
startActivity(intent);

最后,在您的其他应用程序中,您使用以下路径再次加载图片:

Bundle b = getIntent().getExtras();
String picturePath = b.getString("picture");
Bitmap scaledBitmap = decodeSampledBitmapFromFile(picturePath, 150, 150);
于 2013-07-02T03:24:42.653 回答
0

我不知道您是否需要在 String 对象中转换您的位图以尊重您活动中的某些逻辑。如果不是这种情况,那么转换位图 -> String 变得无用,因为Bitmapimplements Parcelable,所以你可以通过 Intent with putExtra(String name, Parcelable value)method 传递它。

您可以在此链接中找到一些代码

例子:

发送意图

Intent intent = new Intent(Home.this, Upload.class);
intent.putExtra("picture", thumbnail);

并从其他活动中检索它

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("picture");

希望能帮助到你

于 2013-07-01T22:06:34.547 回答