Jump to content
  • Advertisement
Sign in to follow this  
grumpyOldDude

List Images, Select and Load Selected image to View From Gallery/Folder

This topic is 533 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In Android, My attempt to list images and load the selected one from a Gallery/Folder images has only resulted in blank screens 

 

connecting to gallery files from xml layout is a mystery to me as (as far as I know) Android only provides "drawable" folders for that. But using drawable folder is hard coding - good for some apps - but not the app I'm writing

 

But the problem I'm having is bigger than just connecting to gallery files from xml layout, the problem is listing (using listview or gridview), selecting desired image and loading selected to view 

 

The code below resulted in a blank screen (I did give read/write permission in manifest).  Anyone know how I can correct my code (or other code) to achieve list, select and load to view,  

 

thanks

(Java)

public class ChooseLoadPhoto extends Activity {
	private int count;
	private Bitmap[] thumbnails;
	private boolean[] thumbnailsselection;
	private String[] arrPath;
	private ImageAdapter imageAdapter;
	ArrayList<String> f = new ArrayList<String>();// list of file paths
	File[] listFile;
	String ImageDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Bonan/";


	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.zchoose_load_dwg);
	    getFromSdcard();
	    GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
	    imageAdapter = new ImageAdapter();
	    imagegrid.setAdapter(imageAdapter);
	
	
	}
	public void getFromSdcard()
	{
	    File file= new File(ImageDir, "MapleBear");
	
	        if (file.isDirectory())
	        {
	            listFile = file.listFiles();
	
	
	            for (int i = 0; i < listFile.length; i++)
	            {
	
	                f.add(listFile[i].getAbsolutePath());
	
	            }
	        }
	}
	
	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.listandselectfromgallery, null);
	            holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
	
	            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;
	}
}

xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView android:id="@+id/thumbImage" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:layout_centerInParent="true" />
	<CheckBox android:id="@+id/itemCheckBox" android:layout_width="wrap_content"
	    android:layout_height="wrap_content" android:layout_alignParentRight="true"
	    android:layout_alignParentTop="true" />
 </RelativeLayout>

Share this post


Link to post
Share on other sites
Advertisement

At this point the best bet is to get friendly with a debugger.  Drop breakpoints and validate all the results.

 

Looking through the code, some questions I would answer with the debugger include

 

* Is getFromSDCard opeining the right directory?

* How many files did it find?

* Are the paths being added to the list correctly?

* Is the GridView created successfully? (You never verify that it is not null, you just use it assuming it is correct.)

* Is the inflator returning an actual value or null?

 

Etc.

 

Also, look in your logs for error coming from the system that you may not have noticed.

 

Step through the code one line at a time and ensure it is what you expect.  Often there are subtle errors, like a file name not exactly matching or a conversion failing internally that you didn't notice.

Share this post


Link to post
Share on other sites
+1 frob, if I could upvote I would but for whatever reason my browser won't do it.

GrumpyOldDude, if you could answer frobs dot points then that will eliminate some potential problems.

Share this post


Link to post
Share on other sites
GrumpyOldDude, if you could answer frobs dot points then that will eliminate some potential problems.

 

I do agree to a large extent. In fact at the time of writing frob's current upvote  was from me. As I do find his advice very helpful. Though what makes it very tricky is that at the xml layout level, there is no way of me knowing how to connect the gallery folder resource to the code. And the debugger wouldn't help in this case. If I don't get that right first the debugger might falsify the output of the code

 

I could connect if it was in the drawable (which can't be the case here) android:src="@drawable/image" , but I can't do this with Gallery/folder/files. Nonetheless I know its doable except haven't  found that specific examples yet. There may be other layers of problems in the code which I'm currently tracking with the debugger (though like I said at this stage I might get false positives due to xml layout logical errors)

 

Many Thanks

 

Edit Does anyone know how to connect external folders from xml layout to activity code? ( the debugger isn't helping in this specific case)

Share this post


Link to post
Share on other sites

there is no way of me knowing how to connect the gallery folder resource to the code. And the debugger wouldn't help in this case.

By connecting the gallery folder I assume you just mean loading all files in the folder defined at getFromSdcard(). In that case then yes there is a way to test this, which is put break points in that function and see if the file object is null or not. That will at least get you started as to why you are seeing blank. Put a breakpoint in onCreate(). Is the activity actually being created?

I don't believe this is something you can do from a layout file alone, Java code will be needed. And use of the debugger is vital.

Once you have determined your objects aren't null, try calling "notifyDataSetChanged()" on your adapter at the end of getFromSdcard(). See the developer documentation for info https://developer.android.com/reference/android/widget/BaseAdapter.html

Also put a breakpoint in ImageAdapter::getView(). If your code is not reaching that then your files are not being rendered.

Instead of rolling your own gallery/file selector have you thought about using the built in one using "Intent.ACTION_GET_CONTENT"? See this link to get you started http://stackoverflow.com/questions/17765265/difference-between-intent-action-get-content-and-intent-action-pick

Share this post


Link to post
Share on other sites

 

Instead of rolling your own gallery/file selector have you thought about using the built in one using "Intent.ACTION_GET_CONTENT"? See this link to get you started http://stackoverflow.com/questions/17765265/difference-between-intent-action-get-content-and-intent-action-pick

 

 

This looks like it would work.

Also you can do a file dir list and create a plist with the file contents and bang you can now reference them.

Share this post


Link to post
Share on other sites

 

 

Instead of rolling your own gallery/file selector have you thought about using the built in one using "Intent.ACTION_GET_CONTENT"? See this link to get you started http://stackoverflow.com/questions/17765265/difference-between-intent-action-get-content-and-intent-action-pick

 

 

This looks like it would work.

Also you can do a file dir list and create a plist with the file contents and bang you can now reference them.

 

 

Yeah since I saw @[member='Nyssa']'s link i've been investigating with some code samples. I've been able to browse files in gallery but problem is

1.  the code parameters only allow going straight to  the root,  i.e. I can't specify a subfolder directory in gallery. And

2.  also very importantly i can't load a selected image file to the program. I was thinking I can use intent.getPutExtra(...)   and getExtras( ..) , Intent.getParcelableExtra( ... )  -- like it works when pass bitmap, array bytes between activities

 

So don't yet know if this would let me load a jpeg into code (as bitmap). I Need to be able to load a selected image into program

Share this post


Link to post
Share on other sites
The output of that intent (once th user has selected a file) is a URI. The uri is a path to a file. You can then load and manipulate the data in that file however you please.

But if "Intent.ACTION_GET_CONTENT" doesn't fit your needs then probably best not to shoehorn it in and instead get your original plan to work.

Share this post


Link to post
Share on other sites
The output of that intent (once th user has selected a file) is a URI. The uri is a path to a file. You can then load and manipulate the data in that file however you please.

 

I think this might be the most efficient and most direct way to do this, except that i find it hard to get how Intent outputs a file path. From my limited know-how, Intent is used to transfer and receive data with  putExtra() and getExtras()

Ex

Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("CONTENT_TYPE", "*/*");
//intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(intent,PICKFILE_RESULT_CODE); 

  ^                   

how do i get a file path from Intent? where is this output?

 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!