将图像保存到内部存储,获取并在图库应用程序中打开它

时间:2016-09-27 17:52:13

标签: java android image android-gallery android-internal-storage

我想将图像保存在手机的内存中,然后在默认图像查看器中将其打开。画廊以黑色屏幕打开。查看Android设备监视器上的图片,位于以下路径:" /data/data/com.myapp.myappname/app_ImageTest/test.jpg"将其传输到计算机,图像正常打开。

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
    path_absolute = saveToInternalStorage(bitmap);
}


public void open_photo(View v) {
    Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_VIEW);
    Uri uri = Uri.parse("file://" + path_absolute );
    intent.setDataAndType(uri,"image/*");
    startActivity(intent);
}

private String saveToInternalStorage(Bitmap bitmapImage){
    ContextWrapper cw = new ContextWrapper(getApplicationContext());
    File directory = cw.getDir("ImageTest", Context.MODE_WORLD_READABLE);
    File mypath=new File(directory,"test.jpg");

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(mypath);
        bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return directory.getAbsolutePath();
}

欢迎详细解释,我是Android上的新手。谢谢!

0 个答案:

没有答案