将图像从ImageView保存到设备库

问题描述:

我试图将图像从ImageView保存到设备库。 我想这个代码将图像从ImageView保存到设备库

代码编辑:

URL url = new URL(getIntent().getStringExtra("imageURL")); 
    File f = new File(url.getPath()); 

    addImageToGallery(f.getPath(), this); 

    public static void addImageToGallery(final String filePath, final Context context) 
    { 

     ContentValues values = new ContentValues(); 

     values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis()); 
     values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); 
     values.put(MediaStore.MediaColumns.DATA, filePath); 

     context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
    } 

,但它需要在我没有,因为我是从URL装入文件的文件路径。 如何将图像从ImageView保存到图库中?

感谢..

+0

将文件保存到SD卡的第一.... – 2014-11-03 16:22:17

+0

并非所有设备都可以有SD卡(例如Nexus 5的) – Eladit 2014-11-03 16:23:29

+0

所有器件都具有存储...不要那么辛苦我... – 2014-11-03 16:27:03

简单:

使用此代码:

//to get the image from the ImageView (say iv) 
BitmapDrawable draw = (BitmapDrawable) iv.getDrawable(); 
Bitmap bitmap = draw.getBitmap(); 

FileOutputStream outStream = null; 
File sdCard = Environment.getExternalStorageDirectory(); 
File dir = new File(sdCard.getAbsolutePath() + "/YourFolderName"); 
dir.mkdirs(); 
String fileName = String.format("%d.jpg", System.currentTimeMillis()); 
File outFile = new File(dir, fileName); 
outStream = new FileOutputStream(outFile); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream); 
outStream.flush(); 
outStream.close(); 

此外,为了刷新库并查看那里的图像:

Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
    intent.setData(Uri.fromFile(file)); 
    sendBroadcast(intent); 

另外,还要确保你的应用程序有启用存储权限:

转到设备设置>设备>应用程序>应用程序ion管理器>“您的应用程序”>权限>启用存储权限!

清单权限:

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

感谢您的评论已晚!我实际上找到了解决这个问题的方法,但是你的方法似乎也可以。谢谢! – Eladit 2016-08-15 13:44:50

U可以很容易地从网址上下载文件

File f = new File(url.getPath()); 
+0

那么,现在我有它的路径(或者它只是一个URL?),但是当它保存图片时,我得到一个黑色图片.. – Eladit 2014-11-03 16:38:29

+0

可以请你更新你的问题与代码在哪里你做这一切的东西 – 2014-11-03 16:40:52

+0

是的,只是做:) – Eladit 2014-11-03 16:44:27

ImageView iv = (ImageView)findViewById(R.id.your_image_view); 

然后设置你的形象,当你想要检索/保存

iv.buildDrawingCache(); 

Bitmap bmp = iv.getDrawingCache(); 

然后正常保存图库

File storageLoc = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); //context.getExternalFilesDir(null); 

    File file = new File(storageLoc, filename + ".jpg"); 

    try{ 
     FileOutputStream fos = new FileOutputStream(file); 
     bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
     fos.close(); 

     scanFile(context, Uri.fromFile(file)); 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 


    private static void scanFile(Context context, Uri imageUri){ 
     Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
     scanIntent.setData(imageUri); 
     context.sendBroadcast(scanIntent); 

    } 

当然,请确保您的清单有权写入外部存储。