将位图图像发送到android中的下一个活动并将其转换为字符串

时间:2014-04-05 11:10:30

标签: android android-intent bitmap

我无法将位图转换为字符串并通过发送intent.putExtra将其传递给JSON .....并在接收它时创建下一个活动的方法.... 默认是给出错误,我有jpeg图像... 如何解决这个问题......

Intent   Intent inn=getIntent()
bitmap = (Bitmap) inn.getParcelableExtra("bmp_img");
ByteArrayOutputStream baos=new  ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
        byte [] b=baos.toByteArray();
        String temp=Base64.encodeToString(b, Base64.DEFAULT);

2 个答案:

答案 0 :(得分:0)

public String imageCompression(String image) {
        // to compress image
        BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
        Bitmap b = BitmapFactory.decodeFile(image, bmpFactoryOptions);
        Bitmap out = Bitmap.createScaledBitmap(b, 300, 300, false);

        String[] bits = image.split("/");
        String lastOne = bits[bits.length - 1];
        File imageFile = null;
        File imagesFolder = new File(Environment.getExternalStorageDirectory(),
                "CompressedImages");

        if (imagesFolder.isDirectory()) {

            imageFile = new File(imagesFolder, lastOne);
            FileOutputStream fOut;
            try {
                fOut = new FileOutputStream(imageFile);
                out.compress(Bitmap.CompressFormat.JPEG, 90, fOut);
                fOut.flush();
                fOut.close();
                b.recycle();
                out.recycle();

            } catch (Exception e) {
                e.printStackTrace();
            }

        } else {
            imagesFolder.mkdirs();
            imageFile = new File(imagesFolder, lastOne);
            FileOutputStream fOut;
            try {
                fOut = new FileOutputStream(imageFile);
                out.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
                fOut.flush();
                fOut.close();
                b.recycle();
                out.recycle();

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        File file = new File(imageFile.getPath());
        byte imageData[] = new byte[(int) file.length()];
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
        } catch (FileNotFoundException e3) {

            e3.printStackTrace();
        }
        try {
            fis = new FileInputStream(file);
        } catch (FileNotFoundException e2) {

            e2.printStackTrace();
        }
        BufferedInputStream bis = new BufferedInputStream(fis);
        try {
            bis.read(imageData, 0, imageData.length);
        } catch (IOException e1) {

            e1.printStackTrace();
        }
        imageDataString = Base64.encodeToString(imageData, 0);
        return imageDataString;
    }


Use this methord and for intent passing pass just as String ......

答案 1 :(得分:0)

我假设你从A活动A和B导航到A-> B

在A

  Uri imageUri = Uri.parse("android.resource://your.package/drawable/fileName");
  Intent intent = new Intent(Intent.ACTION_SEND);
  intent.setType("image/png");
  intent.putExtra(Intent.EXTRA_STREAM, imageUri);
  startActivity(intent)

和B

Intent intent = this.getIntent();

(Uri) getIntent().getExtras().get(Intent.EXTRA_STREAM);