【问题标题】:How do I save my imageView image into Gallery (Android Development)如何将 imageView 图像保存到图库(Android 开发)
【发布时间】:2013-02-28 22:05:43
【问题描述】:

我正在尝试创建一个 onClick 事件,通过单击按钮将图像视图保存到手机图库中,下面是我的代码。它没有保存到图库中,谁能帮我找出原因?

    sharebtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View b) {
            // TODO Auto-generated method stub
            //attempt to save the image

            b = findViewById(R.id.imageView);
            b.setDrawingCacheEnabled(true);
                Bitmap bitmap = b.getDrawingCache();
                //File file = new File("/DCIM/Camera/image.jpg");
                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();
                }

        }
    });

【问题讨论】:

  • 您是否在清单文件中授予了写入权限。

标签: android image imageview save gallery


【解决方案1】:

假设ImageView已经保存了你要保存的图片,首先获取Bitmap

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

然后用下面的代码保存它:-

MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);

别忘了在你的清单中设置这个权限:-

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

【讨论】:

  • 我需要在标题中添加扩展名吗?
  • 这是迄今为止我能找到的最好的解决方案,但它看起来需要实时获得许可,否则它不起作用。对于那些寻求类似方法的人,我建议在实施此解决方案之前添加the code available here
【解决方案2】:

我这样做是为了将图片保存在图库中。

private void saveImageToGallery(){
    imageview.setDrawingCacheEnabled(true);
    Bitmap b = imageview.getDrawingCache();
    Images.Media.insertImage(getActivity().getContentResolver(), b,title, description);
}

insertImage() 将返回 String != null 如果图像已经真正保存。 另外:需要清单中的权限为“android.permission.WRITE_EXTERNAL_STORAGE” 请注意,这会将图像置于画廊中已有图像列表的底部。

希望这会有所帮助。

【讨论】:

  • 有什么方法可以让它显示在图片列表的顶部?
  • 是的,我使用了 samkirton 提供的技巧。 See here。效果很好
  • setDrawingCacheEnabled 已弃用
【解决方案3】:
public static void addImageToGallery(final String filePath, final Context context) {

    ContentValues values = new ContentValues();

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

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

【讨论】:

  • 您基本上复制了另一个人的答案中的代码,但根本没有解释为什么您的答案可能有效。
【解决方案4】:
private void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

看看这个:http://developer.android.com/training/camera/photobasics.html#TaskGallery

【讨论】:

    【解决方案5】:

    您必须将图像保存到媒体提供商。这是一个简单的例子:

    Uri saveMediaEntry(String imagePath,String title,String description,long dateTaken,int orientation,Location loc) {
    ContentValues v = new ContentValues();
    v.put(Images.Media.TITLE, title);
    v.put(Images.Media.DISPLAY_NAME, displayName);
    v.put(Images.Media.DESCRIPTION, description);
    v.put(Images.Media.DATE_ADDED, dateTaken);
    v.put(Images.Media.DATE_TAKEN, dateTaken);
    v.put(Images.Media.DATE_MODIFIED, dateTaken) ;
    v.put(Images.Media.MIME_TYPE, “image/jpeg”);
    v.put(Images.Media.ORIENTATION, orientation);
    File f = new File(imagePath) ;
    File parent = f.getParentFile() ;
    String path = parent.toString().toLowerCase() ;
    String name = parent.getName().toLowerCase() ;
    v.put(Images.ImageColumns.BUCKET_ID, path.hashCode());
    v.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, name);
    v.put(Images.Media.SIZE,f.length()) ;
    f = null ;
    if( targ_loc != null ) {
    v.put(Images.Media.LATITUDE, loc.getLatitude());
    v.put(Images.Media.LONGITUDE, loc.getLongitude());
    }
    v.put(“_data”,imagePath) ;
    ContentResolver c = getContentResolver() ;
    return c.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, v);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-05
      相关资源
      最近更新 更多