如何在android上打开文件

时间:2011-11-27 13:15:46

标签: java android

我是Android和编程的新手。我正在使用android touchpaint api,并使用以下代码保存图形,但显然保存是无用的,无法打开文件。

我想知道是否有人可以帮我处理一些代码。

    // Function saves image to file
public String save(Bitmap mBitmap, boolean showMsg) {
    String filename;
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    filename = sdf.format(date);
    try {

        String path = Environment.getExternalStorageDirectory().toString() + "/modTouchPaint/";
        File file1 = new File(path);
        file1.mkdirs();

        OutputStream fOut = null;
        File file = new File(path, filename + ".jpg");
        fOut = new FileOutputStream(file);

        mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        fOut.flush();
        fOut.close();

        if (showMsg)
            Toast.makeText(this, "Picture saved to " + path + filename + ".jpg", 9000).show();

        return path + filename + ".jpg";
    } catch (Exception e) {
        e.printStackTrace();

        Toast.makeText(this, "Please make sure that SD card is installed", 5000).show();

        return null;
    }
} 

1 个答案:

答案 0 :(得分:0)

Bitmap bitmap = BitmapFactory.decodeFile(pathToFile);

有关详细信息,请参阅documentation