0

当我在画布上绘制并保存它时,它可以正常工作,
但是当我想从 sd 卡加载图片时,我得到了一些错误(但是当我加载我绘制的一张图片并保存它时,它可以正常工作)

加载图片代码

        case R.id.menu_load:
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(intent, REQ_LOAD);*/
        return true;

                    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case REQ_LOAD:

        if (resultCode == RESULT_OK) {
            Uri imageUri = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

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

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            final File imageFile = new File(cursor.getString(columnIndex));
            cursor.close();

            String loadMessge = getResources().getString(R.string.dialog_load);
            final ProgressDialog load = ProgressDialog.show(TpMainActivity.this, "", loadMessge, true);
            Thread thread = new Thread() {
                @Override
                public void run() {
                    //File photos= new File(imageFile);
                    //Bitmap bitmap = decodeFile(imageFile);
                    Bitmap bitmap = Utils.decodeFile(TpMainActivity.this, imageFile);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 90, null);
                    paintView.setBitmap(bitmap);
                    load.dismiss();
                }
            };
            thread.start();
        }
        break;
    }
}

public static Bitmap decodeFile(Context c, File f) {
    Bitmap tmpBitmap = null;
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // The new size we want to scale to
        final int REQUIRED_SIZE = 1024; 

        // Find the correct scale value. It should be the power of 2.
        int tmpWidth = o.outWidth, tmpHeight = o.outHeight;
        int scale = 1;

    while (tmpWidth / 2 > REQUIRED_SIZE || tmpHeight / 2 > REQUIRED_SIZE) { 
            tmpWidth /= 2;
            tmpHeight /= 2;
            scale *= 2;
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;

        tmpBitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);

        // http://sudarnimalan.blogspot.com/2011/09/android-convert-immutable-bitmap-into.html
        // this is the file going to use temporally to save the bytes.
        File file = new File(c.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "tmp");
        file.getParentFile().mkdirs();

        // Open an RandomAccessFile
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

        // get the width and height of the source bitmap.
        int width = tmpBitmap.getWidth();
        int height = tmpBitmap.getHeight();

        // Copy the byte to the file
        // Assume source bitmap loaded using options.inPreferredConfig = Config.ARGB_8888;
        FileChannel channel = randomAccessFile.getChannel();
        MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, width * height * 4);
        tmpBitmap.copyPixelsToBuffer(map);
        // recycle the source bitmap, this will be no longer used.
        tmpBitmap.recycle();
        // Create a new bitmap to load the bitmap again.
        tmpBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        map.position(0);
        // load it back from temporary
        tmpBitmap.copyPixelsFromBuffer(map);
        // close the temporary file and channel , then delete that also
        channel.close();
        randomAccessFile.close();
        file.delete();
    } catch (FileNotFoundException e) {
        Log.e(TpApplication.TAG, "ERROR ", e);
    } catch (IOException e) {
        Log.e(TpApplication.TAG, "ERROR ", e);
    }
    return tmpBitmap;
}
4

1 回答 1

0

要打开图像格式 SD 卡:

mBitmap = BitmapFactory.decodeFile("/sdcard/test.png");
于 2013-01-16T13:23:14.020 回答