从存储中获取文件时出错

时间:2013-12-08 00:06:40

标签: android fileinputstream fileoutputstream

我正在关注教程here

当我在存储位图之后从内部存储器获取位图时:

    public boolean saveImageToInternalStorage(Bitmap default_b) {

    try {
    // Use the compress method on the Bitmap object to write image to
    // the OutputStream
    FileOutputStream fos = mContext.openFileOutput("desiredFilename.png", Context.MODE_PRIVATE);

    // Writing the bitmap to the output stream
    default_b.compress(Bitmap.CompressFormat.PNG, 100, fos);
    fos.close();

    return true;
    } catch (Exception e) {
    Log.e("saveToInternalStorage()", e.getMessage());
    return false;
    }
    }

在一个班级中尝试这样:

   public boolean isSdReadable() {

    boolean mExternalStorageAvailable = false;
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
    // We can read and write the media
    mExternalStorageAvailable = true;
    Log.i("isSdReadable", "External storage card is readable.");
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    // We can only read the media
    Log.i("isSdReadable", "External storage card is readable.");
    mExternalStorageAvailable = true;
    } else {
    // Something else is wrong. It may be one of many other
    // states, but all we need to know is we can neither read nor write
    mExternalStorageAvailable = false;
    }

    return mExternalStorageAvailable;
    }

public Bitmap getThumbnail(String filename) {

    final String APP_PATH_SD_CARD = "/DesiredSubfolderName/";
    final String APP_THUMBNAIL_PATH_SD_CARD = "thumbnails";

    String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + APP_PATH_SD_CARD + APP_THUMBNAIL_PATH_SD_CARD;
    Bitmap thumbnail = null;

    // Look for the file on the external storage
    try {
    if (isSdReadable() == true) {
    thumbnail = BitmapFactory.decodeFile(fullPath + "/" + filename);
    }
    } catch (Exception e) {
    Log.e("getThumbnail() on external storage", e.getMessage());
    }

    // If no file on external storage, look in internal storage
    if (thumbnail == null) {
    try {
    File filePath = Context.getFileStreamPath(filename);
    FileInputStream fi = new FileInputStream(filePath);
    thumbnail = BitmapFactory.decodeStream(fi);
    } catch (Exception ex) {
    Log.e("getThumbnail() on internal storage", ex.getMessage());
    }
    }
    return thumbnail;
    }



@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Try to reuse the views
    ImageView view = (ImageView) convertView;
    @SuppressWarnings("unused")
    boolean checked = (mCheckBox==null)?false:(((CheckBox)  mCheckBox).isChecked());
    // if convert view is null then create a new instance else reuse it
    if (view == null) {
        view = new ImageView(Context);
        Log.d("GridViewAdapter", "new imageView added");
    }
    if(checked = true){
        isSdReadable();
        getThumbnail(toString());
    }
    view.setImageResource(drawables.get(position));
    view.setScaleType(ImageView.ScaleType.CENTER_CROP);
    view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70));
    view.setTag(String.valueOf(position));
    return view;
}

我在Logcat中收到此错误(请注意,这就是显示的全部内容):

12-07 17:00:21.802: I/isSdReadable(30831): External storage card is readable.
12-07 17:00:21.802: I/isSdReadable(30831): External storage card is readable.
12-07 17:00:21.802: E/getThumbnail() on internal storage(30831): /data/data/com.example.awesomefilebuilderwidget/files/com.example.awesomefilebuilderwidget.GridViewAdapter@4052f1c8 (No such file or directory)

所以我知道错误出现在这段编码中:

// If no file on external storage, look in internal storage
    if (thumbnail == null) {
    try {
    File filePath = Context.getFileStreamPath(filename);
    FileInputStream fi = new FileInputStream(filePath);
    thumbnail = BitmapFactory.decodeStream(fi);
    } catch (Exception ex) {
    Log.e("getThumbnail() on internal storage", ex.getMessage());
    }
    }
    return thumbnail;
    }

但我无法弄清楚为什么我会收到这个错误。

为什么?

0 个答案:

没有答案