3

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

Piyush
  • 1,973
  • 1
  • 19
  • 30
  • 1
    before using this u need to send a braodcast to the system to update the gallery otherwise you wont able to successed it, i tried this code and its work very well. – PiyushMishra Jan 31 '12 at 10:26

3 Answers3

1

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);
Community
  • 1
  • 1
0

I think this will help Gallery with Folder Filter

Even when I was trying for this..I couldn't succeeded to find a way for that..had search a lot but no use..

Community
  • 1
  • 1
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • i have also tried same code as you can see ,but it is showing other images also may be i am doing it wrong if you find correct way please reply. – Piyush Dec 08 '11 at 10:15
  • is there anyone who can help me with this. – Piyush Dec 08 '11 at 13:08
  • I dont know any particular person have done this but I have posted your question in 2 chat rooms casualchat & android..hope someone will help you – MKJParekh Dec 08 '11 at 13:17
  • Hello Frankenstein any luck yet – Piyush Dec 09 '11 at 05:50
  • no yar..I just didn't require that feature..so left it..but had tried a lot so I first answered ...That there is no possible way for that..but you can keep trying..cause my words are not final.. Is It Necessary For You to Use Default Intent..cause You can show Particular Folder Images In You View..You can Fetch Them Using MediaStore and Show Them in GridView...but no idea with default view. – MKJParekh Dec 09 '11 at 05:53
  • ayu suggestion for countering out of memory error while displaying images. – Piyush Dec 09 '11 at 06:03
  • yes when you fetch images and store them into array/list of bitmap..before storing them ...scale them to small size 50X50 ..using BitmapFactory.createScaledBitmap() – MKJParekh Dec 09 '11 at 06:05
  • Thanks for suggestion actually problem is scaling is out of the prospect because it is causing blur image i have also tried that but thanks any way – Piyush Dec 09 '11 at 06:08
  • man in gridview show the small image...when click the gridview..to show full image that time..load the image from that path again..this will increase the time and load but..right now this is in my mind only – MKJParekh Dec 09 '11 at 06:12
  • i am doing that only this is letting us see only one image at the time thanks any way. – Piyush Dec 09 '11 at 06:20
0

send broadcast before using this to update the gallery. just copy paste it.

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file:/+ Environment.getExternalStorageDirectory())));
PiyushMishra
  • 5,743
  • 6
  • 37
  • 57