Access pictures from Pictures app in my android app
You can usestartActivityForResult
, passing in an Intent that describes an action you want completed and and data source to perform the action on.
Luckily for you, Android includes an Action for picking things: Intent.ACTION__PICK
and a data source containing pictures: android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI
for images on the local device orandroid.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI
for images on the SD card.
Call startActivityForResult
passing in the pick action and the images you want the user to select from like this:
startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), SELECT_IMAGE);
Then override onActivityResult
to listen for the user having made a selection.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_IMAGE)
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
// TODO Do something with the select image URI
}
}
Once you have the image Uri you can use it to access the image and do whatever you need to do with it.
Android read image files from app
Since your target is API 25, you'll need to add in runtime persmission checks to access the user's media.
See this article for more information: https://developer.android.com/training/permissions/requesting.html
Alternatively, you can goto Android Settings->Apps and find your app and manually grant the app permission to media. This should get you passed your problem, but you'll want to add the runtime permission checks if you plan on distributing the app.
How to access photos and camera in android using one intent
To access camera and photos using one intent
populate a custom dialog's listview with intents from apps that can either take pictures using camera or access photos in the storage like this
private void acquirePicture(){
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes();
WMLP.gravity = Gravity.CENTER;
dialog.getWindow().setAttributes(WMLP);
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setCanceledOnTouchOutside(true);
dialog.setContentView(R.layout.about_dialog);
dialog.setCancelable(true);
lv=(ListView)dialog.findViewById(R.id.listView1);
populateDialogsListView();
dialog.show();
}
Poputate dialog's listview (lv
) with apps that can perform the two actions
public void populateDialogListView(){
PackageManager pm=getPackageManager();
List<ResolveInfo> photoIntents = new ArrayList<>();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(
captureIntent, PackageManager.GET_RESOLVED_FILTER);
final List<ResolveInfo> listGalley = packageManager.queryIntentActivities(photoPickerIntent, PackageManager.GET_RESOLVED_FILTER);
for (ResolveInfo activity : listCam) {
photoIntents.add(activity);
}
for (ResolveInfo activity : listGalley) {
photoIntents.add(activity);
}
Collections.sort(photoIntents,
new ResolveInfo.DisplayNameComparator(pm));
AppAdapter appAdapter = new AppAdapter(pm, photoIntents);
lv.setAdapter(appAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
ResolveInfo launchable=appAdapter.getItem(position);
ActivityInfo activity=launchable.activityInfo;
ComponentName name=new ComponentName(activity.applicationInfo.packageName,
activity.name);
IntentFilter filter = launchable.filter;
int actioncode;
Intent intent = new Intent();
Uri uri;
if(filter.hasAction(Intent.ACTION_PICK)){
actioncode = 1;
uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
intent.setData(uri);
}else{
actioncode = 0;
}
intent.setComponent(name);
startActivityForResult(intent,actioncode);
}
});
}
Since you are creating a custom dialog with a listview, you have to create adapter for that listview(lv
) which will fill the listview with app's name and app's icon
class AppAdapter extends ArrayAdapter<ResolveInfo> {
private PackageManager pm=null;
AppAdapter(PackageManager pm, List<ResolveInfo> apps) {
super(Custom_chooser.this, R.layout.row, apps);
this.pm=pm;
}
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
if (convertView==null) {
convertView=newView(parent);
}
bindView(position, convertView);
return(convertView);
}
private View newView(ViewGroup parent) {
return(getLayoutInflater().inflate(R.layout.row, parent, false));
}
private void bindView(int position, View row) {
TextView label=(TextView)row.findViewById(R.id.label);
label.setText(getItem(position).loadLabel(pm));
ImageView icon=(ImageView)row.findViewById(R.id.icon);
icon.setImageDrawable(getItem(position).loadIcon(pm));
}
Any problem, let me know
How to access an image from the phone's photo gallery?
You have to launch the Gallery App using the built-in Intents
. After that, on your onActivityResult()
, get the path of the selected image and load your image into your ImageView
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/loadimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Load Image"
/>
<TextView
android:id="@+id/targeturi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/targetimage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Your Activity
package com.exercise.AndroidSelectImage;
import java.io.FileNotFoundException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class AndroidSelectImage extends Activity {
TextView textTargetUri;
ImageView targetImage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonLoadImage = (Button)findViewById(R.id.loadimage);
textTargetUri = (TextView)findViewById(R.id.targeturi);
targetImage = (ImageView)findViewById(R.id.targetimage);
buttonLoadImage.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
Uri targetUri = data.getData();
textTargetUri.setText(targetUri.toString());
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
targetImage.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
How to access all pictures on an Android phone?
The best way to do this would be to write a routine that merged the two separate data structures into one data structure then just use that new data structure that you created.
Related Topics
How to Compile C into an Executable Binary File and Run It in Android from Android Shell
Interstitial Admob Ads: "Illegalstateexception: Only Fullscreen Activities Can Request Orientation"
Error:Unexpected Lock Protocol Found in Lock File. Expected 3, Found 0
Android: How to Integrate a Decoder to Multimedia Framework
Remove Padding Around Action Bar Left Icon on Android 4.0+
How to Create a Multilingual Android Application
How to Set Notification with Custom Sound in Android
How to Implement Placeautocompletefragment and Placeautocompleteactivity to Get Place Details
Android:Split the Screen in 2 Equals Parts with 2 Listviews
Android Retrofit - Onprogressupdate for Showing Progress Notification
Android: How to Convert Whole Imageview to Bitmap
How to Iterate Through All Keys of Shared Preferences
Error:(9, 5) Error: Resource Android:Attr/Dialogcornerradius Not Found
Custom Format Edit Text Input Android to Accept Credit Card Number
How Does Bitmap Allocation Work on Oreo, and How to Investigate Their Memory