2014-01-13 4 views
0

есть imagview хочет сохранить его в память вот мой код:Как сохранить изображение из ImageView?

View content = findViewById(R.id.full_image_view); 
content.setDrawingCacheEnabled(true); 
Bitmap bitmap = content.getDrawingCache(); 
File root = Environment.getExternalStorageDirectory(); 
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg"); 
try { 
    root.createNewFile(); 
    FileOutputStream ostream = new FileOutputStream(root); 
    bitmap.compress(CompressFormat.JPEG, 100, ostream); 
    ostream.close(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 


} 

после не сохранения ничего не происходит, и никакого изображения не существует?

ответ

1

Попробуйте это ..

Изменение root.createNewFile(); к cachePath.createNewFile();

File root = Environment.getExternalStorageDirectory(); 
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg"); 
try { 
    cachePath.createNewFile(); 
    FileOutputStream ostream = new FileOutputStream(cachePath); 
    bitmap.compress(CompressFormat.JPEG, 100, ostream); 
    ostream.close(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

EDIT:

FileOutputStream ostream = new FileOutputStream(cachePath); 
+0

образ сохраняется, но его нечитаемым, как его curupted ? – Soheyl

+0

@Soheyl ты изменил это. если не изменить и попробовать 'FileOutputStream ostream = new FileOutputStream (cachePath);' – Hariharan

0

Как насчет использования следующего фрагмента?

Drawable d = imageView.getBackground(); 
Bitmap bitmap = ((BitmapDrawable)d).getBitmap(); 

File file = new File(Environment.getExternalStorageDirectory(), "fileName.ext"); 
outStream = new FileOutputStream(file); 
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
outStream.flush(); 
outStream.close(); 
1

Используйте эту функцию для сохранения в SD-карты:

private void SaveIamge(Bitmap finalBitmap) { 

    String root = Environment.getExternalStorageDirectory().toString(); 
    File myDir = new File(root + "/saved_images");  
    myDir.mkdirs(); 
    Random generator = new Random(); 
    int n = 10000; 
    n = generator.nextInt(n); 
    String fname = "Image-"+ n +".jpg"; 
    File file = new File (myDir, fname); 
    if (file.exists()) file.delete(); 
    try { 
      FileOutputStream out = new FileOutputStream(file); 
      finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 
      out.flush(); 
      out.close(); 

    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
} 

и добавить в манифесте:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

работал идеально для меня! создать произвольное имя изображения и сохранить его на SD-карте. Великий! Спасибо – LikePod

0

1 - вам нужно соответствующее разрешение в amnifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

2- out.flush() проверить из не нулевой ..

3 -

String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + 
          "/yourforlderName"; 
    File dir = new File(file_path); 
if(!dir.exists()) 
    dir.mkdirs(); 
    File file = new File(dir, "yourforlderName" + image + ".png"); 
    FileOutputStream fOut = new FileOutputStream(file); 

    bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut); 
    fOut.flush(); 
    fOut.close(); 

Если вы не создаете каталог на своей SD-карте, то как хранить изображения в SD-карте определенного места? поэтому, пожалуйста, проверьте это ... я надеюсь, что он вам полезен.

0

Чтобы получить растровое из ImageView:

imageview.buildDrawingCache(); 
    Bitmap bm=imageview.getDrawingCache(); 

Чтобы сохранить его в файле:

OutputStream fOut = null; 
    Uri outputFileUri; 
    try { 
    File root = new File(Environment.getExternalStorageDirectory() 
     + File.separator + "folder_name" + File.separator); 
    root.mkdirs(); 
    File sdImageMainDirectory = new File(root, "myPicName.jpg"); 
    outputFileUri = Uri.fromFile(sdImageMainDirectory); 
    fOut = new FileOutputStream(sdImageMainDirectory); 
    } catch (Exception e) { 
    Toast.makeText(this, "Error occured. Please try again later.", 
     Toast.LENGTH_SHORT).show(); 
    } 

    try { 
    bm.compress(Bitmap.CompressFormat.PNG, 100, fOut); 
    fOut.flush(); 
    fOut.close(); 
    } catch (Exception e) { 
    } 

добавить в манифесте:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
Смежные вопросы