[英]Transfer Image picked from gallery to another activity
我正在尝试将画廊或相机选择的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);}
}
}
}
为何不将图像转换为字符串,而不使用文件的路径,然后在其他活动中重新加载它呢? 我的应用程序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);
我不知道您是否需要在String对象中转换位图以尊重您活动中的某些逻辑。 如果不是这种情况,那么转换位图- >字符串成为无用的,因为Bitmap
实现Parcelable
,所以你可以通过意图传递putExtra(String name, Parcelable value)
的方法。
您可以在此链接中找到一些代码
例:
发送意向
Intent intent = new Intent(Home.this, Upload.class);
intent.putExtra("picture", thumbnail);
并从其他活动中检索
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("picture");
希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.