【问题标题】:Saving image from image view to sd card : Android将图像从图像视图保存到 sd 卡:Android
【发布时间】:2013-12-04 09:41:10
【问题描述】:
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 保存为图像。不是原图。您是否对要保存的图像视图进行了一些更改?
  • 是的,我正在通过删除其蓝色和绿色内容来处理检索到的图像。并将图像制作成红色图像。然后我将此红色图像保存回SD卡。
  • 如果Bitmap的宽度和高度大于ImageView的宽度和高度,那么ImageView会调整Bitmap(实际上不是Bitmap,它会将Bitmap转换为BitmapDrawable)的尺寸以适应它的实际大小。所以你会得到小尺寸的图片
  • 我以另一种方式做到了......我在图像视图中检索到图像后立即保存了图像,即没有任何处理。我仍然发现保存的图像分辨率较低..请提供任何建议
  • @Gopal 我无法更改图像视图的高度和宽度,因为它周围有很多东西。有没有什么办法可以在保存时以与原始分辨率相同的分辨率保存回sd卡

标签: android


【解决方案1】:

试试这个代码:

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, reference
【解决方案2】:
   bm.compress(CompressFormat.JPEG, 100, fos);

删除这一行

【讨论】:

  • 如果这样做,我只能看到 jpg 图标。当我打开它时什么都没有...0字节
【解决方案3】:

已解决,这就是我实现从 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-04
    • 1970-01-01
    相关资源
    最近更新 更多