gpt4 book ai didi

android - BitmapFactory decodeFile FileNotFoundException

转载 作者:行者123 更新时间:2023-11-29 14:31:45 27 4
gpt4 key购买 nike

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

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

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

提前致谢

马特。

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);
}
}

.

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;
}
}

.

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

最佳答案

您获取的路径不是图像的真实路径,它是一个 Uri。如果您想将其设置为 ImageView,请将其设置为

imageView.setImageURI(Uri.parse(imagePaths.get(position)));

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

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

关于android - BitmapFactory decodeFile FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24524809/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com