首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BitmapFactory decodeFile FileNotFoundException

BitmapFactory decodeFile FileNotFoundException
EN

Stack Overflow用户
提问于 2014-07-02 07:03:09
回答 6查看 4.6K关注 0票数 1

我有一个应用程序,你可以选择图片从画廊或照片文件夹上的设备。所选文件的路径存储在意图中,因此它们可以在活动之间传递。我通过intent.getDataString()访问路径。

一旦有了所有选定的图像路径,我就将它们存储在ArrayList中,并将其传递给ImageAdapter,以便在ListView中显示。

我得到了一个FileNotFoundException,有人知道为什么吗?

提前感谢

马特。

代码语言:javascript
复制
import java.util.ArrayList;

import uk.co.mobilewebexpert.infowrapsynclibrary.ApplicationObj;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

public class QueuedImagesActivity extends Activity {

    private static final String TAG = QueuedImagesActivity.class.getSimpleName();
    private ImageAdapter adapter;
    private ListView imageList;
    ApplicationObj appObj;
    Intent[] photos;
    String path;

    private ArrayList<String> imagePaths= new ArrayList<String>(); // Edit your code here..

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_listview);

        appObj = (ApplicationObj) getApplication();

        boolean includeBeingProcessed = true;

        try {
             photos = appObj.getQueuedPhotos(includeBeingProcessed);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        for(int i = 0; i < photos.length; i++){

            path = photos[i].getDataString();
            imagePaths.add(path);

            Log.e(TAG, "path in QueuedImagesActivity = " + path);

        }




        imageList= (ListView) findViewById(R.id.listView1);
        adapter= new ImageAdapter(getBaseContext(), imagePaths);
        imageList.setAdapter(adapter);      
    }
}

代码语言:javascript
复制
import java.util.ArrayList;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {

    private static final String TAG = ImageAdapter.class.getSimpleName();

    static class RowItemHolder{
        ImageView imageView;
    }
    private Context context;
    private ArrayList<String> imagePaths= new ArrayList<String>();

    public ImageAdapter(Context baseContext, ArrayList<String> imagePaths) {
    // TODO Auto-generated constructor stub
        this.context= baseContext;
        this.imagePaths= imagePaths;
    }

    @Override
    public int getCount() {
    // TODO Auto-generated method stub
        return imagePaths.size();
    }

    @Override
    public Object getItem(int position) {
    // TODO Auto-generated method stub
            return null;
    }

    @Override
    public long getItemId(int position) {
    // TODO Auto-generated method stub
            return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View view;
    view= convertView;
    RowItemHolder holder = null;
    if(view== null){
            LayoutInflater in =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = in.inflate(R.layout.image_view, parent, false);
            holder= new RowItemHolder();
            holder.imageView=(ImageView) view.findViewById(R.id.imageView1);
        view.setTag(holder);
    } else{
            holder = (RowItemHolder) view.getTag();
    }
    //Edit the code here according to you needs.. 
    //like creating option and converting to Bitmap, 
    //or you can do this job in the main activity.
    //holder.imageView.setImageResource(imagePaths.get(position));

    Log.e(TAG, "imagePaths.get(position) = " + imagePaths.get(position));


    holder.imageView.setImageBitmap(BitmapFactory.decodeFile(imagePaths.get(position)));

    return view;
}
}

代码语言:javascript
复制
07-02 07:51:33.941: E/QueuedImagesActivity(22700): path in QueuedImagesActivity = content://media/external/images/media/7496
07-02 07:51:33.951: E/ImageAdapter(22700): imagePaths.get(position) = content://media/external/images/media/7496
07-02 07:51:33.961: E/BitmapFactory(22700): Unable to decode stream: FileNotFoundException
07-02 07:51:33.971: E/ImageAdapter(22700): imagePaths.get(position) = content://media/external/images/media/7496
07-02 07:51:33.971: E/BitmapFactory(22700): Unable to decode stream: FileNotFoundException
07-02 07:51:33.981: E/ImageAdapter(22700): imagePaths.get(position) = content://media/external/images/media/7496
07-02 07:51:33.981: E/BitmapFactory(22700): Unable to decode stream: FileNotFoundException
07-02 07:51:33.991: E/ImageAdapter(22700): imagePaths.get(position) = content://media/external/images/media/7496
07-02 07:51:33.991: E/BitmapFactory(22700): Unable to decode stream: FileNotFoundException
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2014-07-02 07:10:17

您正在获取的路径不是图像的真实路径,而是要设置它的Uri.If -- ImageView设置它

代码语言:javascript
复制
imageView.setImageURI(Uri.parse(imagePaths.get(position)));

通过传递URI并将其设置为ImageView来获取实路径

代码语言:javascript
复制
private String getRealPathFromURI(Uri contentURI) {
String result;
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file path
    result = contentURI.getPath();
} else { 
    cursor.moveToFirst(); 
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
    result = cursor.getString(idx);
    cursor.close();
}
return result;
}

有关更多信息,请在这里查看Uri to path conversion

票数 5
EN

Stack Overflow用户

发布于 2014-07-02 07:12:38

这是因为intent.getDataString()返回uri字符串。使用intent.getData().getPath()代替。

票数 2
EN

Stack Overflow用户

发布于 2014-07-02 07:14:20

试着这样做,希望这能帮助您解决问题。

代码语言:javascript
复制
  public String getAbsolutePath(Uri uri) {
        if(Build.VERSION.SDK_INT >= 19){
            String id = uri.getLastPathSegment().split(":")[1];
            final String[] imageColumns = {MediaStore.Images.Media.DATA };
            final String imageOrderBy = null;
            Uri tempUri = getUri();
            Cursor imageCursor = getContentResolver().query(tempUri, imageColumns,
                    MediaStore.Images.Media._ID + "="+id, null, imageOrderBy);
            if (imageCursor.moveToFirst()) {
                return imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
            }else{
                return null;
            }
        }else{
            String[] projection = { MediaColumns.DATA };
            Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
            if (cursor != null) {
                int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            } else
                return null;
        }

    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24524809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档