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

标签 android

在下面的代码中,我从 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/

相关文章:

java - 如何使用正则表达式匹配字符串

android - 使用phonegap-2.5.0 创建项目期间出现错误

android - Android Emulator 4 上的相机为 HIGH DEF

android - Glide 模块加载自定义尺寸的 url 图片,带有路径和任何 uri 处理

android - 如何允许用户在 TextView 中选择文本范围(类似于 EditText)

android - Jenkins : ionic 安卓构建失败

java - 无法从 ArrayList<String> 转换为 String[]

java - 工具栏和设置栏之间的小空白

java - XML Pull 解析器异常 - kSOAP2

android - 从 mysql 数据库中检索密码发送到 android+ java webservices 中的电子邮件