将大型图片从图库添加到我的应用

时间:2013-11-06 07:10:44

标签: android bitmap

我的应用在从我的设备库向我的应用添加大尺寸图片时崩溃了。我已经经历了一些解决方案,比如调整大小,缩放等等,但它没有成功。

当我从图库中选择图像时执行以下代码部分

 protected void onActivityResult(int requestCode, int resultCode, 
        Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
        switch(requestCode) { 
            case REQ_CODE_PICK_IMAGE:
                if(resultCode == RESULT_OK){  
                        Uri selectedImage = imageReturnedIntent.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 filePath = cursor.getString(columnIndex);
                        cursor.close();
                        //Convert Bitmap to Byte Array:-
                        Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
                        byte[] byteArray = stream.toByteArray();
                        //Pass byte array into intent:-             
                        Intent intent = new Intent(UserAccount.this,RetrieveImage.class);
                        intent.putExtra("picture", byteArray);
                        startActivity(intent);
            } } }

RetrieveImage.java

         Bundle extras = getIntent().getExtras();
        byte[] byteArray = extras.getByteArray("picture");
        Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
        ImageView img = (ImageView) findViewById(R.id.imageView1);
        img.setImageBitmap(bmp);

我该如何解决这个问题。

3 个答案:

答案 0 :(得分:0)

将此用于缩放:

Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), 
                    THUMBSIZE, THUMBSIZE);

答案 1 :(得分:0)

 InputStream stream = context.getContentResolver().openInputStream(imageUri);

 stream = ctx.getContentResolver().openInputStream(imageUri);
 Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);
 stream.close();


 Use above to get bitmap image from image uri create your options.

答案 2 :(得分:0)

试试此代码

File f = new File("getfile from media");
        if(f.exists())
        {
            FileInputStream is = null;
            try {
                is = new FileInputStream(f);
            } catch (FileNotFoundException e) {
                Log.d("error: ", String.format(
                "ShowPicture.java file[%s]Not Found", fileName));
                return;
            }   
            Bitmap bm = BitmapFactory.decodeStream(is, null, null);                             
            pic.setImageBitmap(bm);

            }