0

我想将图像保存在手机的内存中,然后在默认图像查看器中打开它。画廊以黑屏打开。在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新手。谢谢!

4

0 回答 0