Java 重新编码图像时发生outOfMemory异常

Java 重新编码图像时发生outOfMemory异常,java,android,filestream,Java,Android,Filestream,我想把一幅图像编码到64位 从gallery中选择图像并尝试保存后,我遇到以下错误: outOfMemory异常 有谁能建议如何将此映像无内存错误地发送到base 64吗 MotorImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Intent i = new Intent(

我想把一幅图像编码到64位

从gallery中选择图像并尝试保存后,我遇到以下错误:

outOfMemory异常

有谁能建议如何将此映像无内存错误地发送到base 64吗

        MotorImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });
}


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

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
    Uri selectedImage = data.getData();
    String[] filePathColumn = { MediaStore.Images.Media.DATA };

    Cursor cursor = getContentResolver().query(selectedImage,
            filePathColumn, null, null, null);
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String picturePath = cursor.getString(columnIndex);
    cursor.close();


 //   
   ImageView imageView = (ImageView) findViewById(R.id.imageView1);
    imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));



    String imageString = null;

    try {
        Bitmap bm = BitmapFactory.decodeFile(picturePath);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object  
        bm.recycle();
        byte[] b = baos.toByteArray();
        imageString = Base64.encodeToString(b, Base64.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
    }




    Toast.makeText(getApplicationContext(), imageString, Toast.LENGTH_SHORT).show();

尝试在AndroidManifest上的应用程序标记中使用android:largeHeap=“true”,这样您的应用程序将有更多可用ram,并且不会引发oom异常。

我怀疑您需要缩放和重新采样图像以适应设备上的限制,请尝试类似的操作

// decodes image and scales it to reduce memory consumption
private Bitmap decodeImage(String picturePath) {
    try {
        File file = new File(picturePath);
        // Get image size
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(file), null, opts);

        // The new size we want to scale to
        final int MIN_SIZE = 70;

        // Find the correct scale value.
        int scale = 1;
        while (((opts.outWidth / scale) >> 1) >= MIN_SIZE
                && ((opts.outHeight / scale) >> 1) >= MIN_SIZE) {
            scale <<= 1;
        }

        BitmapFactory.Options opts2 = new BitmapFactory.Options();
        opts2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(file), null, opts2);
    } catch (FileNotFoundException e) {
    }
    return null;
}
//解码图像并对其进行缩放以减少内存消耗
私有位图解码图像(字符串图片路径){
试一试{
文件文件=新文件(picturePath);
//获取图像大小
BitmapFactory.Options opts=新的BitmapFactory.Options();
opts.inJustDecodeBounds=true;
decodeStream(新文件输入流(file),null,opts);
//我们要扩展到的新尺寸
最终整数最小值=70;
//找到正确的比例值。
int标度=1;
而((选择外径/刻度)>>1)>=最小尺寸
&&((选择高度/刻度)>>1)>=最小尺寸){

缩放如果你阅读了android的官方文档,你就会知道这是android的一个常见问题,建议的解决方案是根据你的需要调整图像大小。你也可以参考developer.android了解这一点

你的android版本是什么?不要写入
ByteArrayOutputStream
,写入文件,网络等等cket,…您不能将任意大的数据写入内存。也不要将其编码为
字符串
,因为这也会写入内存。也有输出流。我想将其保存为字符串,因为我会将其存储在sql server数据库中,我应该使用什么?您不需要
字符串
,您可以将其流式传输到您的服务器r也是。具体操作方式取决于服务器和许多其他因素。base64-ing映像确实不是一个好主意,应该避免。请在文件上使用base64 OutputStream。是否可以使用映像路径字符串而不是File@user3245658对不起,我错过了。它是
opts
.Edited.@user3245658我知道。但首先你必须解码它并保存它缩放它,然后编码!:)所以我应该在你的方法之后使用我的原始帖子?@user3245658缩放图像后,你的原始方法应该可以工作;我建议你尝试一种流式解决方案(而不是内存中的解决方案)来保存;但这应该可以减少输出的大小,使其能够工作。