如何从onactivityresult URI路径发送到另一个活动,将其更改为图像 [英] how to send from onactivityresult uri path to another activity and change it to image

查看:211
本文介绍了如何从onactivityresult URI路径发送到另一个活动,将其更改为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在低于code我正在从SD卡的图像,并通过意向发送图像, 不过,我想改变code发送图像的路径,而不是图像本身

In the below code I am getting an image from sd card , and sending the image through an intent, however I want to change the code to send the path of the image , and not the image itself

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

我也试过,但没有奏效:

Also I tried this but it didn't work:

 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路径

but how to receive the uri path

的getURI()。getintent()? 然后获取图像

编辑 -

我有误差 NullPointerException异常URI字符串此处

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

这是在code

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

我送的意图是这样

i am sending intent like this

文件路径= data.getData();   意图I =新的意图(这一点,                         AddImage.class);   i.putExtra(的ImagePath,文件路径);  startActivity(我);

filePath = data.getData(); Intent i = new Intent(this, AddImage.class); i.putExtra("imagepath", filePath); startActivity(i);

推荐答案

AddImage活动

AddImage 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();
        ....
   }

这篇关于如何从onactivityresult URI路径发送到另一个活动,将其更改为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆