gpt4 book ai didi

android - 从 GridView 中的特定 SDCard 文件夹加载图像

转载 作者:行者123 更新时间:2023-11-29 16:05:31 24 4
gpt4 key购买 nike

我正在尝试开发一个应用程序,它在 SDCard 的一个文件夹中创建图像并显示它们(“MyCreatedImages”文件夹现在显示在图库中),现在我正在尝试从一个给定的文件夹中加载所有图像 GridView 布局。我正在使用以下代码,但是由于我无法理解的原因,我不断收到 NPE。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.activity_my_image_fragment,
container, false);

// Set up an array of the Thumbnail Image ID column we want
String[] projection = { MediaStore.Images.Thumbnails._ID };

// Create the cursor pointing to the SDCard
cursor = getActivity().getContentResolver().query(
Uri.parse("file://" + Environment.getExternalStorageDirectory()+ File.separator + "MyCreatedImages"), projection,
null, null, MediaStore.Images.Thumbnails._ID);

// Get the column index of the Thumbnails Image ID
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

GridView gridview = (GridView) root.findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this.getActivity()));
//this.registerForContextMenu(gridview);
return root;
}

还有我的 Gridview 适配器:

private class ImageAdapter extends BaseAdapter {
private Context context;

public ImageAdapter(Context localContext) {
context = localContext;
}

public int getCount() {
return cursor.getCount();
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(context);
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = cursor.getInt(columnIndex);
// Set the content of the image based on the provided URI
picturesView.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
"" + imageID));
picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
picturesView.setPadding(8, 8, 8, 8);
picturesView
.setLayoutParams(new GridView.LayoutParams(100, 100));
} else {
picturesView = (ImageView) convertView;
}
return picturesView;
}
}

打开此 fragment 后,我收到以下 LogCat 错误:

09-23 16:25:06.286: E/AndroidRuntime(28497): FATAL EXCEPTION: main
09-23 16:25:06.286: E/AndroidRuntime(28497): java.lang.NullPointerException
09-23 16:25:06.286: E/AndroidRuntime(28497): at com.meme.hdmeme.MyMemeFragment.onCreateView(MyMemeFragment.java:51)
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.os.Handler.handleCallback(Handler.java:615)
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.os.Handler.dispatchMessage(Handler.java:92)
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.os.Looper.loop(Looper.java:137)
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.app.ActivityThread.main(ActivityThread.java:4921)
09-23 16:25:06.286: E/AndroidRuntime(28497): at java.lang.reflect.Method.invokeNative(Native Method)
09-23 16:25:06.286: E/AndroidRuntime(28497): at java.lang.reflect.Method.invoke(Method.java:511)
09-23 16:25:06.286: E/AndroidRuntime(28497): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
09-23 16:25:06.286: E/AndroidRuntime(28497): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
09-23 16:25:06.286: E/AndroidRuntime(28497): at dalvik.system.NativeStart.main(Native Method)

第 51 行是:

    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

最佳答案

试试这个函数从 sd 卡中获取图像:

 ArrayList<String> f = new ArrayList<String>();   // list of available files in  path
File[] listFile;

public void getSdcardImages()
{
File file= new File(android.os.Environment.getExternalStorageDirectory(),"MyFolder");

if (file.isDirectory())
{
listFile = file.listFiles();


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

f.add(listFile[i].getAbsolutePath());

}
}
}

使用权限:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

然后通过 Adapter 在 Grid View 中设置:

 GridView imggrid = (GridView) findViewById(R.id.imgGrid);
imgAdapter = new ImageAdapter();
imggrid .setAdapter(imgAdapter);

如下设置适配器:

  public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;

public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
return f.size();
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(
R.layout.gallery, null);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumb);

convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}


Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
holder.imageview.setImageBitmap(myBitmap);
return convertView;
}
}

class ViewHolder {
ImageView imageview;


}

关于android - 从 GridView 中的特定 SDCard 文件夹加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18963152/

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