将图像从图像视图保存到SD卡:Android

Asw*_*thy 4 android

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
                {}               
        }
        });
Run Code Online (Sandbox Code Playgroud)

首先,我从SD卡检索图像到分辨率的图像视图(640 x 480).然后我再次将图像从图像视图保存到SD卡.但保存的图像分辨率为(188x113).任何人都可以建议我如何以相同的分辨率保存.任何建议都会很明显.

小智 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();
}
Run Code Online (Sandbox Code Playgroud)