I tested the code on a MeiZu mobile phone. The code runs smoothly, but it shows "cannot search any pic". My mobile phone has some pictures, so I want to ask why this is happening.
Here is my code:
mainactivity:
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>
I think I see the problem now.
String path=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
File parentFile=new File(path);
(...)
if(parentFile.list()==null){
The File parentFile looks like it's actually the file itself, and not the parent (directory). Therefore parentFile.list() returns null as a regular file cannot have files in it.
If you instead do something like this, I think it should work:
File currentFile = new File(path);
File parentFile = currentFile.getParentFile();
(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With