将图片从图库转移到另一个活动

时间:2013-07-01 20:56:05

标签: android

我正在尝试将由图库或相机选择的m图像发送到另一个活动,但似乎没有发生任何事情,我已将Bitmap转换为字符串以通过意图发送它但似乎永远不会调用intent

请帮助我,我做错了什么?

这是我的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);}

                }
            }

            }

2 个答案:

答案 0 :(得分: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 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);

答案 1 :(得分:0)

我不知道你是否需要在String对象中转换你的位图以尊重你活动中的某些逻辑。如果不是这种情况,那么转换位图 - >字符串变得无用,因为Bitmap实现了Parcelable,因此您可以使用putExtra(String name, Parcelable value)方法通过意图传递它。

您可以在此link

找到一些代码

示例:

发送意图

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

并从其他活动中检索

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

希望有所帮助