如何将ImageView保存为图片?

10

我有一个带有共享意图的ImageView(非常有效,可以显示出可与之共享图像的所有应用程序),但是由于图片在我的手机上没有路径,因此无法共享照片。我该如何将ImageView保存到我的手机上?以下是我的代码。

 public void taptoshare(View v)
{  
    View content = findViewById(R.id.myimage);
    content.setDrawingCacheEnabled(true);
        Bitmap bitmap = content.getDrawingCache();
        File file = new File("/DCIM/Camera/image.jpg");
        try 
        {
            file.createNewFile();
            FileOutputStream ostream = new FileOutputStream(file);
            bitmap.compress(CompressFormat.JPEG, 100, ostream);
            ostream.close();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }


    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    Uri phototUri = Uri.parse("/DCIM/Camera/image.jpg");
    shareIntent.setData(phototUri);
    shareIntent.setType("image/*");
    shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
    startActivity(Intent.createChooser(shareIntent, "Share Via"));

}   
}

更新 好的,我搞定了。现在我有一个新问题,如何将这个图片保存到一个新文件夹中?


一个新的问题应该分开提出,因为你不能给一个问题选择多个答案。但无论如何,File类有一个mkdirs()方法,用于确保指定的文件夹存在。 - AlbeyAmakiir
好的,谢谢AlbeyAmakiir!记住了,以后会注意发布内容! - dabious
2个回答

4

在保存和加载时,首先需要获取系统的根路径。以下是我会这样做。

File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");

我已经添加了,但仍然无法正常工作。有任何建议吗? - dabious

1
我遇到了几个解决方案,但都没有解决这个问题。
以下是适合我的解决方案。需要注意的是,您需要将图像存储在共享或非应用程序私有位置(http://developer.android.com/guide/topics/data/data-storage.html#InternalCache)中。
许多建议说要将其存储在“私有”缓存位置Apps,但显然其他外部应用程序无法访问此位置,包括正在使用的通用Share File意图。当您尝试此操作时,它会运行,但例如Dropbox会告诉您该文件不再可用。
/*第1步-使用下面的文件保存函数本地保存位图文件。*/
localAbsoluteFilePath = saveImageLocally(bitmapImage);

/* 步骤2 - 共享非私有的绝对文件路径到共享文件意图 */

if (localAbsoluteFilePath!=null && localAbsoluteFilePath!="") {

    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    Uri phototUri = Uri.parse(localAbsoluteFilePath);

    File file = new File(phototUri.getPath());

    Log.d("file path: " +file.getPath(), TAG);

    if(file.exists()) {
        // file create success

    } else {
        // file create fail
    }
    shareIntent.setData(phototUri);
    shareIntent.setType("image/png");
    shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
    activity.startActivityForResult(Intent.createChooser(shareIntent, "Share Via"), Navigator.REQUEST_SHARE_ACTION);
}   

/* 保存图片功能 */

    private String saveImageLocally(Bitmap _bitmap) {

        File outputDir = Utils.getAlbumStorageDir(Environment.DIRECTORY_DOWNLOADS);
        File outputFile = null;
        try {
            outputFile = File.createTempFile("tmp", ".png", outputDir);
        } catch (IOException e1) {
            // handle exception
        }

        try {
            FileOutputStream out = new FileOutputStream(outputFile);
            _bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.close();

        } catch (Exception e) {
            // handle exception
        }

        return outputFile.getAbsolutePath();
    }

/* 步骤 3 - 处理分享文件意图的结果。需要删除临时文件等。 */

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

            // deal with this with whatever constant you use. i have a navigator object to handle my navigation so it also holds all mys constants for intents
        if (requestCode== Navigator.REQUEST_SHARE_ACTION) {
            // delete temp file
            File file = new File (localAbsoluteFilePath);
            file.delete();

            Toaster toast = new Toaster(activity);
            toast.popBurntToast("Successfully shared");
        }


    }   

/* 工具 */

public class Utils {
    //...
    public static File getAlbumStorageDir(String albumName) {

        // Get the directory for the user's public pictures directory.
        File file = 
            new File(Environment.getExternalStorageDirectory(), albumName);
        if (!file.mkdirs()) {
            Log.e(TAG, "Directory not created");
        }
        return file;
    }   
    //...
}

我希望这能帮助到某个人。


我在第二步出错了(什么是utils?):文件输出目录 = Utils.getAlbumStorageDir(Environment.DIRECTORY_DOWNLOADS); - Milon
@DinIslamMilon,抱歉,我已经有一段时间没有处理这个问题了。我已经添加了自定义的 utils 静态类,它可以获取用户图片目录。 - wired00

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接