gpt4 book ai didi

android - 将从图库中选取的图像传输到另一个 Activity

转载 作者:行者123 更新时间:2023-11-29 01:52:01 27 4
gpt4 key购买 nike

我正在尝试将由画廊或相机选择的 m 图像发送到另一个 Activity ,但似乎没有任何反应,我已将位图转换为字符串以通过 Intent 发送它,但似乎从未调用 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);}

}
}

}

最佳答案

为什么不将图像转换为字符串,为什么不使用文件路径并在其他 Activity 中重新加载它呢?我的应用程序遇到了同样的问题 Pic4Share我就这样解决了。

在从图库加载图像的 Activity 中:

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);

关于android - 将从图库中选取的图像传输到另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17413550/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com