从图片视图保存图片到SD卡:Android

4
testButton.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View v) 
        {
                    imageView.setImageBitmap(Bitmap);
                    imageView.buildDrawingCache();
                    Bitmap bm =imageView.getDrawingCache();

               Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Punch");
                imagesFolder.mkdirs(); 
                String fileName = "image"  + ".jpg";
                File output = new File(imagesFolder, fileName);
                Uri uriSavedImage = Uri.fromFile(output);
                imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
                OutputStream fos = null;

                try {
                    fos = getContentResolver().openOutputStream(uriSavedImage);
                    bm.compress(CompressFormat.JPEG, 100, fos);
                    fos.flush();
                    fos.close();
                    } 
                catch (FileNotFoundException e) 
                    {
                    e.printStackTrace();
                    } 
                catch (IOException e)
                {
                    e.printStackTrace();
                } 
                finally
                {}               
        }
        });

首先我从SD卡中检索到一张分辨率为(640 x 480)的图片并将其放入图像视图中。然后我将该图像再次保存到SD卡中。但是保存的图像分辨率为(188x113)。请问有什么建议可以使保存的图像具有相同的分辨率?任何建议都将不胜感激。


你正在将ImageView保存为图像,而不是原始图像。你是否对ImageView进行了一些想要保存的更改? - Aman Gautam
是的,我正在处理所检索到的图像,通过去除其蓝色和绿色内容,并将图像转换为红色图像。然后我将这个红色图像保存回SD卡。 - Aswathy
如果Bitmap的宽度和高度大于ImageView的宽度和高度,则ImageView将调整Bitmap(实际上不是Bitmap,而是将其转换为BitmapDrawable)的尺寸以适应其实际大小。因此,您将获得小尺寸图像。 - Gopal Gopi
我用另一种方法做了。我在获取图像视图后立即保存了图像,即没有经过任何处理。但仍然发现保存的图像分辨率较低。请问有什么建议? - Aswathy
@Gopal,我无法更改图像视图的高度和宽度,因为周围放置了许多东西。是否有可能在保存时将其保存回SD卡,并具有与原始分辨率相同的分辨率? - Aswathy
显示剩余4条评论
3个回答

6

试一下这段代码:

BitmapDrawable btmpDr = (BitmapDrawable) ivPic.getDrawable();
Bitmap bmp = btmpDr.getBitmap();

/*File sdCardDirectory = Environment.getExternalStorageDirectory();*/
try
{
    File sdCardDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + "MeeguImages");
    sdCardDirectory.mkdirs();

    imageNameForSDCard = "image_" + String.valueOf(random.nextInt(1000)) + System.currentTimeMillis() + ".jpg";

    File image = new File(sdCardDirectory, imageNameForSDCard);
    FileOutputStream outStream;

    outStream = new FileOutputStream(image);
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, outStream); 
    /* 100 to keep full quality of the image */
    outStream.flush();
    outStream.close();



    //Refreshing SD card
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
}
catch (Exception e) 
{
    e.printStackTrace();
    Toast.makeText(ViewImage.this, "Image could not be saved : Please ensure you have SD card installed " +
                                                                            "properly", Toast.LENGTH_LONG).show();
}

Android不允许广播android.intent.action.MEDIA_MOUNTED参考链接 - Bugs Happen

0
解决了,这是我成功保存 ImageView 中图像的方法。
/*Variable which holds Image*/
    {ImageView ivBanner = "Initialise It :)";
     FileOutputStream fileOutputStream = openFileOutput("ImageName" + ".jpg", MODE_PRIVATE);

     Bitmap bitmap = convertToBitMap(ivBanner.getDrawable(),ivBanner.getWidth(),ivBanner.getHeight());
     bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fileOutputStream);
     File file = getFileStreamPath("ImageName" + ".jpg");
     File f = file.getAbsoluteFile();
     /*Utilise your path whatever way you want*/
     String localPath = f.getAbsolutePath();}

     /* Covert Drawable to Bitmap*/
    private Bitmap convertToBitMap(Drawable drawable, int width, int height) {
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0,0,width,height);
    drawable.draw(canvas);
    return bitmap;
}

0
   bm.compress(CompressFormat.JPEG, 100, fos);

移除这行


如果我这样做,我只能看到JPG图标。当我打开它时,什么也没有...0字节。 - Aswathy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接