【问题标题】:how to send from onactivityresult uri path to another activity and change it to image如何从onactivityresult uri路径发送到另一个活动并将其更改为图像
【发布时间】:2015-09-15 11:17:09
【问题描述】:

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

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

我正在发送这样的意图

filePath = data.getData(); 意图 i = 新意图(这个, AddImage.class); i.putExtra("imagepath", filePath); 开始活动(i);

【问题讨论】:

  • 最好以字符串形式发送路径或保存在共享首选项中
  • @quicklearner 所以你是说从 onactivityresult 获取路径,将其保存在 sharedpreference 中,然后将其带到另一个活动?
  • 在第一个活动中保存,然后在下一个活动中获取
  • String uri = getIntent().getStringExtra("imagePath");
  • @quicklearner 和 arun,你能给我看一个简单的例子吗?

标签: android


【解决方案1】:

添加图片活动

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

【讨论】:

  • 但我需要压缩图像,如何添加?位图输出 = Bitmap.createScaledBitmap(bitmap, 500, 500, false); // 位图是图像 ByteArrayOutputStream stream = new ByteArrayOutputStream(); out.compress(Bitmap.CompressFormat.JPEG, 60, 流); out.recycle();
  • 得到图片后我想缩小尺寸
  • 我在应用你的代码时出错,检查我的编辑,你能找出问题吗?
  • 去掉out.recycle()
  • 但是如果我想加载另一张图片,回收很重要stackoverflow.com/questions/3823799/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-05
  • 1970-01-01
  • 2023-03-28
  • 2016-11-04
  • 2012-07-20
相关资源
最近更新 更多