0

我在我的 android 应用程序中使用 ImageView 在这里我显示来自 web 服务的图像,所以我使用 UrlImageViewHelper。我想将此图像存储到 android Gallery 文件中。

我的图像像:

    String Images = dataExtra.get("images").toString();
    System.out.println("image URL"+Images);     
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(Images);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);

我试过这样..但它不工作。任何人都可以帮助我如何将这些图像存储到 Android Gallery 中?

4

2 回答 2

1

我得到了这个问题的解决方案,这是我的答案

private void saveImagesIntoGallery(){
        BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
        Bitmap bitmap = drawable.getBitmap();
       // File sdCardDirectory = Environment.getExternalStorageDirectory();// its stores under sdcard not in a specific path
        String sdCardDirectory = Environment.getExternalStorageDirectory().toString()+"/Pictures/";
        String url = arrayForImages[i].toString();
        String file = url.substring(url.lastIndexOf('/')+1);
        System.out.println("PATH NAME"+sdCardDirectory);
        File image = new File(sdCardDirectory, file);
        boolean success = false;

        // Encode the file as a PNG image.
        FileOutputStream outStream;
        try {

            outStream = new FileOutputStream(image);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
            /* 100 to keep full quality of the image */
            outStream.flush();
            outStream.close();
            success = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (success) {
            Toast.makeText(getApplicationContext(), "Image saved with success",
                    Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(),
                    "Error during image saving", Toast.LENGTH_LONG).show();
        }
    }
于 2013-01-31T10:57:45.553 回答
0

您确定画廊是存储网络服务图像的最佳场所吗?

如果您想保存到内部存储:

public void saveBitmap(String name, Bitmap bitmap){

        if(bitmap!=null && name!=null){
            FileOutputStream fos;

            if(bitmap!=null){
                try {
                    fos = openFileOutput(name, Context.MODE_PRIVATE);
                    bitmap.compress(CompressFormat.JPEG, 90, fos);
                } catch (FileNotFoundException e) {}
            }
        }
    }

到画廊,我这里没有例子。但是搜索一下;)

于 2013-01-30T13:54:55.190 回答