gpt4 book ai didi

android - 如何从onactivityresult uri路径发送到另一个 Activity 并将其更改为图像

转载 作者:行者123 更新时间:2023-11-29 20:25:49 26 4
gpt4 key购买 nike

在下面的代码中,我从 sd 卡获取图像,并通过 Intent 发送图像,但是我想更改代码以发送图像的路径,而不是图像本身

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {

//file name
filePath = data.getData();
try {
// Bundle extras2 = data.getExtras();
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
byte imageInByte[] = stream.toByteArray();
Intent i = new Intent(this, AddImage.class);
i.putExtra("image", imageInByte);
startActivity(i);
} catch (IOException e) {
e.printStackTrace(); } } }




and here I am receiving the image



byte[] byteArray = getIntent().getByteArrayExtra("image");
// encodedImage = Base64.encodeToString(byteArray, Base64);
bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView imageview = (ImageView) findViewById(R.id.imageView);
imageview.setImageBitmap(bmp);

我也试过这个但是没用:

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {

filePath = data.getData();

Intent i = new Intent(this, AddImage.class);
i.putExtra("imagepath", filePath);
startActivity(i);
} catch (IOException e) {
e.printStackTrace(); } }}

但是如何接收uri路径

geturi().getintent() ?然后获取图像

编辑--

我在这里得到错误nullpointerexception uri string

    img.setImageURI(Uri.parse(imagePath ));

这是代码

  String imagePath = getIntent().getStringExtra("imagePath");
ImageView img=(ImageView) findViewById(R.id.imageView);
img.setImageURI(Uri.parse(imagePath ));

Bitmap bitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();
Bitmap out = Bitmap.createScaledBitmap(bitmap, 500, 500, false);
// bitmap is the image
ByteArrayOutputStream stream = new ByteArrayOutputStream();
out.compress(Bitmap.CompressFormat.JPEG, 60, stream);
out.recycle();

/* byte[] byteArray = getIntent().getByteArrayExtra("image");

// encodedImage = Base64.encodeToString(byteArray, Base64);
// bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);*/
// ImageView imageview = (ImageView) findViewById(R.id.imageView);
img.setImageBitmap(out);

我正在发送这样的 Intent

文件路径 = data.getData(); Intent 我=新的 Intent (这个, 添加图片类); i.putExtra("图像路径", 文件路径); 启动 Activity (i);

最佳答案

添加图片 Activity

   onCreate(...)
{
....
String imagePath = getIntent().getStringExtra("imagePath");
ImageView img=(ImageView) findViewById(R.id.imageView1);
img.setImageURI(Uri.parse(imagePath ));

Bitmap bitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();
Bitmap out = Bitmap.createScaledBitmap(bitmap, 500, 500, false);
// bitmap is the image
ByteArrayOutputStream stream = new ByteArrayOutputStream();
out.compress(Bitmap.CompressFormat.JPEG, 60, stream);
img.setImageBitmap(out);
bitmap.recycle();
....
}

关于android - 如何从onactivityresult uri路径发送到另一个 Activity 并将其更改为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32584648/

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