Android Open gallery to folder and slide through images - android

I'm able to open Gallery in a specific folder and image; however, when I try to slide through the images that functionality does not seem to work. Since I'm opening the Gallery app I figured that Gallery should handle this functionality. Anyone have any ideas? Can't seem to figure out what to do.
Below is my code
public OpenGalleyToSpecificFolder( Context _context, String path, String fileName){
mContext = _context;
mMediaScannerConnection = null;
mPath = path;
mFileName = fileName;
}
public void OpenGallary( ){
File folder_name = new File(mPath);
allFiles = folder_name.listFiles();
for(int i = allFiles.length - 1; i > 0; --i){
if(allFiles[i].getName().equals(mFileName)){
index = i;
break;
}
}
if(mMediaScannerConnection == null)
mMediaScannerConnection = new MediaScannerConnection(mContext, this);
mMediaScannerConnection.connect();
}
#Override
public void onMediaScannerConnected() {
mMediaScannerConnection.scanFile(allFiles[index].getAbsolutePath(), null);
}
#Override
public void onScanCompleted(String path, Uri uri) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
mContext.startActivity(intent);
mMediaScannerConnection.disconnect();
}

There are thousands of Android device models. There will be hundreds of different "gallery apps" pre-installed across those models, let alone other apps that support ACTION_VIEW that users install themselves. What those apps do, in response to your Intent, is up to the developers of those apps, not you.
If you want a specific look-and-feel to browsing images, implement your own image-browsing UI in your app.

Related

Opening shared image via Gmail Intent

I have the following code that correctly attaches the image to the email and sends:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Set tht type to image/* and add the extra text field for the message to send
sharingIntent.setType(Application2.instance().getResString(R.string.share_intent_type_text_image));
sharingIntent.putExtra(Intent.EXTRA_TEXT, String.format(Application2.instance().getResString(R.string.share_intent_body_question), question.question));
if (destFile != null)
{
Uri uri = Uri.fromFile(destFile);
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
((ActivityMain) getActivity()).startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
R.string.share_intent_type_text_image is defined as "image/png"
destFile is an image grabbed from the external cache directory of the app, (((ActivityMain) getActivity()).getExternalCacheDir()
However, when I attempt to open the file in Gmail, a dialog appears that says: Info - No app can open this attachment for viewing. I've downloaded the file via my PC and the extension comes up as .File. I can open it with paint and other image viewers.
Anyone experience this before?
Considering the FileProvider problems, and also because I wanted to implement a max cache size for collected temp files, I went with a ContentProvider solution and it works a treat. Basically, you're allowed to use your internal cache without any problem but still provide third party apps with a URI they can use to reference your temporary files you want to share with them. Because you use your internal cache, there will be no unnecessary WRITE_EXTERNAL_STORAGE permission to ask for.
The added max cache size limit (that you can remove from the class by simply deleting everything from checkSize() to the end of the class, for instance, if you can make sure you delete all files directly after sharing, so they won't remain on the device) works by checking the cumulated max size upon each call and clearing up half the cache (deleting the oldest files) if necessary.
public class TemporaryFile extends ContentProvider {
private static final long MAX_SIZE = 512 * 1024;
// commented out on purpose so that you don't forget to rewrite it...
// public static final String AUTHORITY = "com.example.tempfile";
private UriMatcher uriMatcher;
#Override
public boolean onCreate() {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, "*", 1);
return true;
}
#Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
if (uriMatcher.match(uri) == 1) {
final String file = getContext().getCacheDir() + File.separator + uri.getLastPathSegment();
return ParcelFileDescriptor.open(new File(file), ParcelFileDescriptor.MODE_READ_ONLY);
}
else
throw new FileNotFoundException(uri.toString());
}
#Override
public int update (Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return 0;
}
#Override
public int delete (Uri uri, String selection, String[] selectionArgs) {
return 0;
}
#Override
public Uri insert(Uri uri, ContentValues values) {
return null;
}
#Override
public String getType(Uri uri) {
return null;
}
#Override
public Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
return null;
}
public static File getFile(Context context, String prefix, String extension) throws IOException {
checkSize(context);
File file = File.createTempFile(prefix, extension, context.getCacheDir());
file.setReadable(true);
file.deleteOnExit();
return file;
}
public static Uri getPublicUri(File file) {
return Uri.withAppendedPath(Uri.parse("content://" + AUTHORITY), file.getName());
}
public static void checkSize(Context context) throws IOException {
File dir = context.getCacheDir();
if (getDirSize(dir) > MAX_SIZE)
cleanDir(dir, MAX_SIZE / 2);
}
private static long getDirSize(File dir) {
long size = 0;
for (File file : dir.listFiles())
if (file.isFile())
size += file.length();
return size;
}
private static void cleanDir(File dir, long atLeast) {
long deleted = 0;
File[] files = dir.listFiles();
Arrays.sort(files, new Comparator<File>() {
public int compare(File f1, File f2) {
return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
}
});
for (File file : files) {
deleted += file.length();
file.delete();
if (deleted >= atLeast)
break;
}
}
}
Using it couldn't be simpler, just call
File file = TemporaryFile.getFile(this, "prefix", ".extension");
whenever you want to create a new file and
TemporaryFile.getPublicUri(file)
whenever you want to get a public Uri to the file, eg. to pass it to an intent as data or Intent.EXTRA_STREAM.
Being a provider, don't forget to add the necessary manifest entry, either:
<provider
android:name=".TemporaryFile"
android:authorities="com.example.tempfile"
android:exported="true"
tools:ignore="ExportedContentProvider" >
</provider>
This works but requires external storage and the relating permissions. When downloading an app, a dialog will show that the app is requesting to be able to read/write data which may turn users away. Use the FileProvider as Simon suggested in my initial post if that's a concern.
Useful links:
https://developer.android.com/reference/android/support/v4/content/FileProvider.html
I attempted to use the File Provider as Simon suggested in my initial post to no avail. I received a NullPointerException on the following line:
final ProviderInfo info = context.getPackageManager()
.resolveContentProvider(authority, PackageManager.GET_META_DATA);
I was unable to track the problem after following the guide at:
https://developer.android.com/reference/android/support/v4/content/FileProvider.html
as well as the other thread at:
How to use support FileProvider for sharing content to other apps?
At this point I realized there is no file type set for the images being used. I simply added .png to the files and the attachments work correctly in Gmail as well as the previous apps that already worked.
I provided the following code if anyone was curious how I shared an internal file. It's not complete and does not handle errors completely but it may be useful for someone as a start.
// Copy image file to external memory and send with the intent
File srcFile = getImage();
File destDir = new File(((ActivityMain) getActivity()).getExternalCacheDir(),
Application2.instance().getResString(R.string.temporary_external_image_path));
if(!destDir.exists())
{
destDir.mkdirs();
}
if(destDir != null && srcFile != null)
{
File destFile = new File(destDir, srcFile.getName());
if (!destFile.exists())
{
try
{
Application2.instance().copy(srcFile, destFile);
}
catch (IOException e)
{
if (BuildConfig.DEBUG) Log.e("Failed to copy file '" + srcFile.getName() + "'");
}
}
if (destFile != null)
{
Uri uri = Uri.fromFile(destFile);
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
((ActivityMain) getActivity()).startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
}

Making a GridView take images from a specific folder

I am a newbie to android app making and I am still a beginner with a little knowledge . I have been trying to make an android app that works like a gallery , but it only displays images under a specific folder. For the UI , I am starting with only a GridView (or TwoWayGridView which is derived from the latter) , and have been trying to let this GridView take its contents from this folder .
I have made this folder and copied an image to it for testing and failed. No image was displayed .Plus I am not very familiar with Cursors and ListAdapters . Somethings that I'm sure that are correct are permissions , manifest , and layout of the activity.Moreover , I believe my problem is around URIs . Please check my code below :
Some namings:
Uri contentUri;
Cursor mImageCursor;
TwoWayGridView mImageGrid;
ListAdapter mAdapter;
String sdCard = Environment.getExternalStorageDirectory().getAbsolutePath();
onCreate method :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
File motherDirectory = new File(sdCard+"/Favory");
if(!motherDirectory.exists()){
motherDirectory.mkdir();
}
MediaScannerConnection.scanFile(this, new String[]{motherDirectory.getAbsolutePath()} ,null, new MediaScannerConnection.OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
// TODO Auto-generated method stub
contentUri = uri ;
initGrid(uri);
}
});
}
initGrid(Uri) method :
private void initGrid(Uri folderUri) {
mImageCursor = this.getContentResolver().query(folderUri,
ImageThumbnailAdapter.IMAGE_PROJECTION, null, null,
MediaStore.Images.ImageColumns.DISPLAY_NAME);
mImageGrid = (TwoWayGridView) findViewById(R.id.gridview);
mAdapter = new ImageThumbnailAdapter(this, mImageCursor);
mImageGrid.setAdapter(mAdapter);
mImageGrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(TwoWayAdapterView<?> parent, View v, int position, long id) {
Log.i(TAG, "showing image: " + mImageCursor.getString(ImageThumbnailAdapter.IMAGE_NAME_COLUMN));
Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
Thanks for your help , and please if there is an easier alternative way of doing this tell me, I care for the results more than the methods now . If you need anything or any more information please tell me in the comments below . Thanks again !
To Read the Files of a folder you can use this function ( from this post ):
String directoryName = Environment.getExternalStorageDirectory().toString()+"/YourFolder/";
public ArrayList<File> listf(String directoryName, ArrayList<File> files) {
File directory = new File(directoryName);
// get all the files from a directory
File[] fList = directory.listFiles();
for (File file : fList) {
Log.e("path : "," "+file);
if (file.isFile()) {
files.add(file);
} else if (file.isDirectory()) {
listf(file.getAbsolutePath(), files);
}
}
return files;
}
Then you should load this list of files to your GridView Adapter, i suggest you use Universal Image Loader
You just give your file path and Adapter ImageVIew at that position
loadImageUtil.loadBitmapToImageView(imageView, youArrayList.get(position));
For more informations how to use this library you can see examples, there is an example with grid view gridView

can we open download folder via. intent?

Actually, I need to open the default Download folder from my application. Is it possible? If yes, then please provide some reference.
I am able to get the path of Download folder with the help of:
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
Any help will be well appreciated.
You can show the recent downloads activity with the following Intent
startActivity(new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));
Available since API 9
Samsung solution:
public static void openDownloads(#NonNull Activity activity) {
if (isSamsung()) {
Intent intent = activity.getPackageManager()
.getLaunchIntentForPackage("com.sec.android.app.myfiles");
intent.setAction("samsung.myfiles.intent.action.LAUNCH_MY_FILES");
intent.putExtra("samsung.myfiles.intent.extra.START_PATH",
getDownloadsFile().getPath());
activity.startActivity(intent);
}
else activity.startActivity(new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));
}
public static boolean isSamsung() {
String manufacturer = Build.MANUFACTURER;
if (manufacturer != null) return manufacturer.toLowerCase().equals("samsung");
return false;
}
public static File getDownloadsFile() {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
}
Found by decompiling the Samsung My Files app dex and seeing how the MainActivity deals with intents.
public static final String MYFILES_NOTIFICATION_LAUNCH_INTENT_NAME = "samsung.myfiles.intent.action.LAUNCH_MY_FILES";
public static final String NOTIFICATION_START_PATH = "samsung.myfiles.intent.extra.START_PATH";
public static final String START_PATH = "FOLDERPATH";
if (Constant.MYFILES_NOTIFICATION_LAUNCH_INTENT_NAME.equals(intent.getAction())) {
String startNotificationPath = intent.getStringExtra(Constant.NOTIFICATION_START_PATH);
if (startNotificationPath != null) {
Bundle newArgument = new Bundle();
newArgument.putString(Constant.START_PATH, startNotificationPath);
if (new File(startNotificationPath).exists()) {
startBrowser(513, newArgument);
}
}
intent.removeExtra(Constant.NOTIFICATION_START_PATH);
}
I had similar problem showing files in Downloads in Samsung and LG and i just added file-parameter "downloaded" for DownloadManager and file showing in Downloads:
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
downloadManager.addCompletedDownload(file.getName(), file.getName(), true,
"application", file.getPath(), file.length(), true);

Retrieving a Content Uri from Camera Intent

I am firing an Intent for the camera to take a photo as follows:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Uri myPhoto = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"the_photo.jpg"));
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, myPhoto);
startActivityForResult(cameraIntent, INTENT_GET_PICTURE_FROM_CAMERA);
Now I want to get the thumbnail for this photo to show. If I store the "myPhoto" Uri, I can see exactly the file on the file system. However, to use the Media Store API, I need the Uri on the Content Uri form. Is there anyway to retrieve this from the intent the camera gives back and if not how can I convert my file system Uri to the android content Uri.
Note, I know I can just take the bitmap and decode it, downsampling as I go. This is not what I want. I can query all the thumbnails and I see it exists, so I want to get it from the system.
Thanks
I resolved it by performing a Media Scan on the item. From there I could get the content Uri (the extra complication of the handler in the code below is because I needed my result to be handled on the UI thread as it manipulated views):
private Uri mCameraFilePath;
private Context mContext;
...
private void handlePictureFromCamera(Intent data) {
MyMediaScannerClient client = new MyMediaScannerClient(
mCameraFilePath.getPath(), new Handler(), new OnMediaScanned() {
#Override
public void mediaScanned(Uri contentUri) {
//Here I have the content uri as its now in the media store
}
});
MediaScannerConnection connection = new MediaScannerConnection(mContext, client);
client.mConnection = connection;
connection.connect();
}
private interface OnMediaScanned {
public void mediaScanned(Uri contentUri);
}
/**
* This is used to scan a given file (image in this case) to ensure it is added
* to the media store thus making its content uri available. It returns the
* content form Uri by means of a callback on the supplied handler's thread.
*/
private class MyMediaScannerClient implements MediaScannerConnectionClient {
private final String mPath;
private MediaScannerConnection mConnection;
private final OnMediaScanned mFileScannedCallback;
private final Handler mHandler;
public MyMediaScannerClient(String path, Handler handler, OnMediaScanned fileScannedCallback) {
mPath = path;
mFileScannedCallback = fileScannedCallback;
mHandler = handler;
}
#Override
public void onMediaScannerConnected() {
mConnection.scanFile(mPath, null);
}
#Override
public void onScanCompleted(String path, final Uri uri) {
mConnection.disconnect();
mHandler.post(new Runnable() {
#Override
public void run() {
mFileScannedCallback.mediaScanned(uri);
}
});
}
}
AS i commented, i needed something similar (to get " content://media/external/images/xxx " uri format) and your solution worked pretty well. Thanks you!
But i found a shorter way to do it (maybe it doesn't work for you, but for someone else).
Using the same intent formulation as yours (first post), but with the "CAMERA_REQUEST"
inside onActivityResult
getContentResolver().notifyChange(mCapturedImageURI, null);
ContentResolver cr = getContentResolver();
Uri uriContent = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), photo.getAbsolutePath(), null, null));
and getting the 'uriContent' in 'content://media/external/images/xxx' format.

Android Camera Intent woes

Hope someone may give some pointers (or an out right answer)...
Simple app, take an image using the built-in camera app, save the image to a separate application. Be done.
Problem: The camera application saves the image in the default app location (/mnt/sdcard/external_sd/DCIM/Camera) as well as my custom path (in code below).
Both files are exactly the same except for the file name. The external_sd file (the one I want gone) is saved with dashes (-) vs my custom file path saved with underscores. File sizes are exactly the same.
How can I stop this double image issue?
Is there an extra intent option I'm missing?
Or am I doing this completely wrong, missing something?
I'm using a Galaxy S Vibrant.
Code snippet:
private static Uri _outputFileUri;
private static File _file;
private ImageView _image;
private SimpleDateFormat _simpleDateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
_takePicture = (Button) findViewById(R.id.takePicture);
_takePicture.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
_file = new File(Environment.getExternalStorageDirectory() +
"/Android/data/my own folder/files/",
_simpleDateFormat.format(new Date()).toString() +
".jpg");
_outputFileUri = Uri.fromFile(_file);
_intent.putExtra(MediaStore.EXTRA_OUTPUT, _outputFileUri);
startActivityForResult(_intent, CAMERA_ACTIVITY);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Activity cancelled", Toast.LENGTH_LONG).show();
return;
}
switch (requestCode) {
case CAMERA_ACTIVITY:
if (resultCode == RESULT_OK) {
try{
Bitmap b = MediaStore.Images.Media.getBitmap(getContentResolver(), _outputFileUri);
_image.setImageBitmap(b);
_image.invalidate();
}
catch(Exception e){
e.printStackTrace();
}
}
break;
}
}
This is device-dependent behavior. My observation is that HTC devices do not have this duplication problem, but Samsung devices do.
Please remove the following lines:
_file = new File(Environment.getExternalStorageDirectory() +
"/Android/data/my own folder/files/",
_simpleDateFormat.format(new Date()).toString() +
".jpg");
_outputFileUri = Uri.fromFile(_file);
_intent.putExtra(MediaStore.EXTRA_OUTPUT, _outputFileUri);
Also update the code to get the image from intent:
Bitmap b = (Bitmap) data.getExtras().get("data");
_image.setImageBitmap(b);
_image.invalidate();
This way picture wouldn't be saved on sd card or default location.
I had the same problem and gave up. Sometime later I found out that I was not getting it anymore and I'm not sure what change I made to my code, but I think that it was MediaStore's fault (check my unsolved question: Weird camera Intent behavior)
As you already have the image URI, why don't you use it to set the ImageViews' bitmap?
// void setImageURI(Uri uri)
_image.setImageBitmap(_outputFileUri);
I had this issue and here is how i solved it :
File createImageFile() throws IOException{
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String filename = "IMG_"+timestamp+"_";
File image = File.createTempFile(filename,".jpg",mGalleryFolder );
if (image.length() == 0 ){
boolean delete = image.delete();
}
mLocation = image.getAbsolutePath();
return image;
}
It's not exactly solving but works for me ;)

Categories

Resources