无法使用我自己编码的应用程序搜索任何图片



我在米苏手机上测试了代码。该代码运行顺利,但显示"无法搜索任何图片"。我的手机有一些图片,所以我想问为什么会发生这种情况。

这是我的代码:

主动脉:

public class MyActivity extends Activity {
    private TextView textView_name;
    private TextView textView_count;
    private Set<String> dirPaths=new HashSet<String>();
    private int MaxCount;
    private File  MaxdirPath;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            iuni();
        }
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView_name= (TextView) findViewById(R.id.id_name);
        textView_count= (TextView) findViewById(R.id.id_count);
        new Thread(){
            @Override
            public void run() {
                SearchPicture();
            }
        }.start();
    }
    private void iuni() {
        if(MaxdirPath==null){
            Toast.makeText(this,"cannot search any pic!",Toast.LENGTH_SHORT).show();
            return;
        }
        textView_name.setText(MaxdirPath.getName());
        textView_count.setText(MaxCount+"");
    }
    private void SearchPicture() {
        if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            Toast.makeText(this,"wrong",Toast.LENGTH_SHORT).show();
            return;
        }
        Uri uri= MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            ContentResolver cr=this.getContentResolver();
        Cursor cursor=cr.query(uri,null,MediaStore.Images.Media.MIME_TYPE+"=? or "+MediaStore.Images.Media.MIME_TYPE+"=?",new String[]{"image/jpeg","image/png"},MediaStore.Images.Media.DATE_MODIFIED);
        while(cursor.moveToNext()){
            String path=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            File parentFile=new File(path);
            if(parentFile==null){continue;}
            String dirPath=parentFile.getAbsolutePath();
            if(dirPaths.contains(dirPath)){
                continue;
            }
            else{
                dirPaths.add(dirPath);
                if(parentFile.list()==null){
                    continue;
                }
                int count=parentFile.list(new FilenameFilter() {
                    @Override
                    public boolean accept(File dir, String filename) {
                        if(filename.endsWith(".jpg")||filename.endsWith(".png")||filename.endsWith("jpeg")){
                            return  true;
                        }
                        return  false;
                    }
                }).length;
                if(count>MaxCount){
                    MaxCount=count;
                    MaxdirPath=parentFile;
                }
            }
        }
        cursor.close();
        handler.sendEmptyMessage(0x110);
    }
}

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           android:id="@+id/id_name"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="5dp"/>
    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
            android:id="@+id/id_count"
            android:layout_alignParentRight="true"
            android:layout_marginRight="5dp"/>
</RelativeLayout>

我想我现在看到了问题。

String path=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
File parentFile=new File(path); 
(...)
if(parentFile.list()==null){

File parentFile看起来实际上是文件本身,而不是父(目录)。因此, parentFile.list()返回null作为常规文件,其中没有文件。

如果您做这样的事情,我认为它应该有效:

File currentFile = new File(path);
File parentFile = currentFile.getParentFile();
(...)

您必须在使用它之前将光标移至第一个位置。只有此后,您才能通过其内容导航。

否则,cursor.moveToNext()将返回false

尝试将代码更改为以下内容:

private void SearchPicture() {
    ...
    Cursor cursor=cr.query(uri,null,MediaStore.Images.Media.MIME_TYPE+"=? or "+MediaStore.Images.Media.MIME_TYPE+"=?",new String[]{"image/jpeg","image/png"},MediaStore.Images.Media.DATE_MODIFIED);
    if(cursor.moveToFirst()) {
        do{
        } while(cursor.moveToNext());
    }
    cursor.close();
    ...
}

最新更新