[英]Transfer Image picked from gallery to another activity
I am trying to send m image which is selected by either gallery or camera to another activity but nothing seems to be happening, I have converted the Bitmap to string to send it over the intent but it seems that the intent never gets invoked. 我正在尝试将画廊或相机选择的m图像发送到另一个活动,但是似乎什么都没有发生,我已经将位图转换为字符串以通过意图发送它,但似乎从未调用该意图。
Please help me what am i doing wrong? 请帮助我,我在做什么错?
this is my onActivityResult() Code 这是我的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);}
}
}
}
Instead of converting the image to string, why don't you work with the path to the file and reload it again in the other activity? 为何不将图像转换为字符串,而不使用文件的路径,然后在其他活动中重新加载它呢? I was facing the same issue with my app Pic4Share and I solved it in that way.
我的应用程序Pic4Share遇到了同样的问题,并且以这种方式解决了。
In the activity that loads the image from the gallery: 在从图库加载图像的活动中:
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();
}
}
}
}
Then you can pass the string with the path using the intent: 然后,您可以使用意图将字符串与路径一起传递:
Intent intent = new Intent(Home.this, Upload.class);
Bundle b = new Bundle();
b.putString("picture", stringPath);
intent.putExtras(b);
startActivity(intent);
Finally, in your other application you load the picture again using the path: 最后,在其他应用程序中,您将使用以下路径再次加载图片:
Bundle b = getIntent().getExtras();
String picturePath = b.getString("picture");
Bitmap scaledBitmap = decodeSampledBitmapFromFile(picturePath, 150, 150);
I don't know if u need to transform your bitmap in a String object to respect some logic in your activity. 我不知道您是否需要在String对象中转换位图以尊重您活动中的某些逻辑。 If this is not the case then the conversion bitmap -> String became useless because
Bitmap
implements Parcelable
, so you could pass it via intent with putExtra(String name, Parcelable value)
method. 如果不是这种情况,那么转换位图- >字符串成为无用的,因为
Bitmap
实现Parcelable
,所以你可以通过意图传递putExtra(String name, Parcelable value)
的方法。
You can find some code at this link 您可以在此链接中找到一些代码
Example: 例:
Send the intent 发送意向
Intent intent = new Intent(Home.this, Upload.class);
intent.putExtra("picture", thumbnail);
and retrieve it from the other activity 并从其他活动中检索
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("picture");
Hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.