Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to open specific folders images using android default gallery

I want to open images of specific folder in my program using default android gallery application. i have used this code given by the piyush mishra in a post but the problem i have written below the code

public class GalleryActivity extends Activity implements MediaScannerConnectionClient{
    /** Called when the activity is first created. */
     public String[] allFiles;
     private String SCAN_PATH ;
     private static final String FILE_TYPE = "images/*";

     private MediaScannerConnection conn;
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

         File folder = new File("/sdcard/images/");
         allFiles = folder.list();
      //   uriAllFiles= new Uri[allFiles.length];
         for(int i=0;i<allFiles.length;i++)
         {
             Log.d("all file path"+i, allFiles[i]+allFiles.length);
         }
       //  Uri uri= Uri.fromFile(new File(Environment.getExternalStorageDirectory().toString()+"/yourfoldername/"+allFiles[0]));
         SCAN_PATH=Environment.getExternalStorageDirectory().toString()+"/images/"+allFiles[0];
         Log.e("SCAN PATH", "Scan Path " + SCAN_PATH);
         Button scanBtn = (Button)findViewById(R.id.scanBtn);
         scanBtn.setOnClickListener(new OnClickListener(){
         @Override       
         public void onClick(View v) {
             startScan();
         }});
         }
         private void startScan()
         {
         Log.d("Connected","success"+conn);
         if(conn!=null)
         {
         conn.disconnect();
         }
         conn = new MediaScannerConnection(this,this);
         conn.connect();
         }
     @Override
     public void onMediaScannerConnected() {
         Log.d("onMediaScannerConnected","success"+conn);
         conn.scanFile(SCAN_PATH, FILE_TYPE);    
     }
     @Override
     public void onScanCompleted(String path, Uri uri) {
         try {
             Log.d("onScanCompleted",uri.toString() + "success"+conn);
             if (uri != null)                
             {

             Intent intent = new Intent(Intent.ACTION_DEFAULT);
             intent.setData(uri);
             startActivity(intent);
             }
             } finally 

             {
             conn.disconnect();
             conn = null;
             }


}
 }

But this code is also shows other images present on device

like image 360
Piyush Avatar asked Nov 19 '25 17:11

Piyush


1 Answers

I have the same problem, I would like to launch the gallery into a specific folder with Videos and Pictures.

With this I always seem to get: ActivityNotFoundException

Based on other posts, I can launch the gallery, but not into a specific folder, nor can I launch both videos and images. Pulling my hair out on this one. Have been trying to implement a grid view with thumbnails, which could be an option. But why go through the trouble of creating, threading, and caching thumbnails, when that is something the gallery app does.

Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);

intent.setData(uri);
intent.setType("image/*");

startActivityForResult(intent, 0);
like image 161
user1991359 Avatar answered Nov 22 '25 09:11

user1991359



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!