unable to upload video through action_send intent in android 4.3 - android

i am able to upload video through this code till 4.1.2 version of android. but in 4.3 this fails.
calling shareVideo() method android displays a list of apps, but in case of selecting
youtube from list, youtube app opens then stops without any message and in case of selecting instagram, instagram app crashes. i am not able to upload video through any App including facebook also.
Please tell me what is the issue??
Thanks in advance.
public void shareVideo(View view){
new Handler().post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
ContentValues content = new ContentValues(4);
content.put(MediaStore.Video.VideoColumns.DATE_ADDED,
System.currentTimeMillis() / 1000);
content.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
content.put(MediaStore.Video.Media.DATA, filename);
Uri videoURI = getBaseContext().getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, content);
//Uri videoURI = Uri.fromFile(new File(filename));
Intent intent = new Intent(Intent.ACTION_SEND);
//intent.setAction(Intent.ACTION_SEND);
intent.setType("video/mp4");
intent.putExtra(MediaStore.EXTRA_OUTPUT, videoURI);
try {
startActivity(Intent.createChooser(intent,"Upload video via:"));
} catch (android.content.ActivityNotFoundException ex) {
}
}
});
}

After 4.1 android changed the mechanism of sd card path. now i can upload the the video by getting uri through the id of video file.
here is my code
public static String getVideoContentUriFromFilePath(Context ctx, String filePath) {
ContentResolver contentResolver = ctx.getContentResolver();
String videoUriStr = null;
long videoId = -1;
Log.d("first log","Loading file " + filePath);
// This returns us content://media/external/videos/media (or something like that)
// I pass in "external" because that's the MediaStore's name for the external
// storage on my device (the other possibility is "internal")
Uri videosUri = MediaStore.Video.Media.getContentUri("external");
Log.d("second log","videosUri = " + videosUri.toString());
String[] projection = {MediaStore.Video.VideoColumns._ID};
// TODO This will break if we have no matching item in the MediaStore.
Cursor cursor = contentResolver.query(videosUri, projection, MediaStore.Video.VideoColumns.DATA + " LIKE ?", new String[] { filePath }, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
videoId = cursor.getLong(columnIndex);
Log.d("third log","Video ID is " + videoId);
cursor.close();
if (videoId != -1 ) videoUriStr = videosUri.toString() + "/" + videoId;
return videoUriStr;
}
and on click of your upload button
use this method-
public void shareVideo(View view){
new Handler().post(new Runnable() {
#Override
public void run() {
String newPath=getVideoContentUriFromFilePath(ShareVideoActivity.this, videoPath);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Title");
//intent.setAction(Intent.ACTION_SEND);
intent.setType("video/mp4");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(newPath));
try {
startActivity(Intent.createChooser(intent,"Upload video via:"));
} catch (android.content.ActivityNotFoundException ex) {
}
}
}
});
}

Related

Share an image with a content provider in Android app

I'm developing an Android app that is a gallery of images in which the images are downloaded from internet for display on the screen of smathphone. Images are displayed one at a time and the application has a button to share the image that is displayed.
Following the directions I've found in some StackOverflow post which indicated that the right way to share an image was using a ContentProvider I have implemented the following code that works to share the images of certain applications (eg Twitter, Gmail ...) but does not work for others (Facebook, Yahoo, MMS ...).
Then I show the code used hoping you can help me get the correct implementation to share images in all applications.
Initially I capture the button press to share:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_share) {
// I get the image being displayed on the screen
View root = getView();
ImageView imageView = (ImageView) root.findViewById(R.id.image);
Drawable imageToShareDrawable = imageView.getDrawable();
if (imageToShareDrawable instanceof BitmapDrawable) {
// I convert the image to Bitmap
Bitmap imageToShare = ((BitmapDrawable) imageToShareDrawable).getBitmap();
// Name of de image extracted from a bean property
String fileName = quote.getImage();
// I keep the image in the folder "files" of internal storage application
TempInternalStorage.createCachedFile(fileName, imageToShare, getActivity().getApplicationContext());
// I start the Activity to select the application to share the image after the intent Built with the method "getDefaultShareIntent"
startActivity(getDefaultShareIntent(fileName));
} else {
Toast.makeText(getActivity().getApplicationContext(), "Please wait, the quote is being downloaded", Toast.LENGTH_SHORT).show();
}
}
return true;
}
The method for saving the image to the internal storage of the application is as follows:
public static void createCachedFile(String fileName, Bitmap image, Context context) {
try {
File file = new File(context.getFilesDir(), fileName);
if (!file.exists()) {
FileOutputStream fos = new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
}
} catch (Exception e) {
Log.e("saveTempFile()", "**** Error");
}
}
The method that constructs the Intent to share it:
private Intent getDefaultShareIntent(String fileName) {
final Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Test text");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://" + CachedFileProvider.AUTHORITY + File.separator + "img" + File.separator + fileName));
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
return shareIntent;
}
Finally ContentProvider code is as follows:
public class CachedFileProvider extends ContentProvider {
private static final String CLASS_NAME = "CachedFileProvider";
public static final String AUTHORITY = "com.example.appname.cachefileprovider";
private UriMatcher uriMatcher;
#Override
public boolean onCreate() {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, "img/*", 1);
return true;
}
#Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
String LOG_TAG = CLASS_NAME + " - openFile";
Log.i(LOG_TAG, "Called with uri: '" + uri + "'." + uri.getLastPathSegment());
switch (uriMatcher.match(uri)) {
case 1:
String fileLocation = getContext().getFilesDir() + File.separator + uri.getLastPathSegment();
ParcelFileDescriptor image = ParcelFileDescriptor.open(new File(fileLocation), ParcelFileDescriptor.MODE_READ_ONLY);
return image;
default:
Log.i(LOG_TAG, "Unsupported uri: '" + uri + "'.");
throw new FileNotFoundException("Unsupported uri: " + uri.toString());
}
}
#Override
public int update(Uri uri, ContentValues contentvalues, String s, String[] as) {
return 0;
}
#Override
public int delete(Uri uri, String s, String[] as) {
return 0;
}
#Override
public Uri insert(Uri uri, ContentValues contentvalues) {
return null;
}
#Override
public String getType(Uri uri) {
return null;
}
#Override
public Cursor query(Uri uri, String[] projection, String s, String[] as1, String s1) {
MatrixCursor c = null;
Log.i(">>>> projection", java.util.Arrays.toString(projection));
String fileLocation = getContext().getFilesDir() + File.separator + uri.getLastPathSegment();
File file = new File(fileLocation);
long time = System.currentTimeMillis();
c = new MatrixCursor(new String[] { "_id", "_data", "orientation", "mime_type", "datetaken", "_display_name" });
c.addRow(new Object[] { 0, file, 0, "image/jpeg", time, uri.getLastPathSegment() });
return c;
}
#Override
public String[] getStreamTypes(Uri uri, String mimeTypeFilter) {
return null;
}
}
I have found that when the image is sharing some applications only call the method "query" (these are where the code does not work and I can not share the image) while there are others that also call the method "query" also call the method "openFile" and these do work and I can share the image.
I hope you can help me, thank you very much in advance.
As #Sun Ning-s comment noted some "share target apps" can handle URI-s starting with "content://.." which you have implemented.
Other apps handle file uri-s starting with "file://...." so you have to implement a 2nd share menue "share as file"
private Intent getFileShareIntent(String fullPathTofile) {
final Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + fullPathTofile));
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
return shareIntent;
}
You can use the android app intentintercept to find out what other "share source apps" provide.

How to send a photo to Instagram using my Android app?

My app takes photos and I want to share it on Instagram.
My app save the image in this directory
File storagePath = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera/tubagram");
Now I'm trying to get the last picture I took to share in Instagram using this code
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");
final ContentResolver cr = getContentResolver();
final String[] p1 = new String[] {MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATE_TAKEN};
Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC");
if (c1.moveToFirst() ) {
Log.i("Test", "last picture (" + c1.getString(0) + ") taken on: " + new Date(c1.getLong(1)));
}
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+Environment.getExternalStorageDirectory() + "/DCIM/Camera/tubagram/" + c1.getString(0)));
shareIntent.setPackage("com.instagram.android");
c1.close();
startActivity(shareIntent);
I receive a Toast with this error message "Unable to download file".
This Toast is sent by Instagram.
I tried to use this link example - share a photo in instagram - but didn't work.
I solved my problem.
I add this line after the camera.takePicture.
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
This line does a "refresh" and after the phone recognizes the news photos saved on your phone.
And I made some changes on my method
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");
final ContentResolver cr = getContentResolver();
final String[] p1 = new String[] {
MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.TITLE, MediaStore.Images.ImageColumns.DATE_TAKEN
};
Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC");
if (c1.moveToFirst() ) {
Log.i("Test", "last picture (" + c1.getString(1) + ") taken on: " + new Date(c1.getLong(2)));
}
Log.i("Image path", "file://"+Environment.getExternalStorageDirectory()+ "/Tubagram/" + c1.getString(1) + ".png");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+Environment.getExternalStorageDirectory()+ "/Tubagram/" + c1.getString(1)+".png"));
shareIntent.setPackage("com.instagram.android");
c1.close();
startActivity(shareIntent);
And with this another method I verify if the Instagram is installed on the phone
private boolean verifyInstagram(){
boolean installed = false;
try {
ApplicationInfo info = getPackageManager().getApplicationInfo("com.instagram.android", 0);
installed = true;
} catch (NameNotFoundException e) {
installed = false;
}
return installed;
}
put this code in your button click listener it will redirect you into app and make sure your device have installed Instagram app.
String type = "image/*";
imageview.buildDrawingCache();
Bitmap bmap = imageview.getDrawingCache();
Uri bmpUri = getLocalBitmapUri(bmap);
Intent share = new Intent(Intent.ACTION_SEND);
if (Utils.isPackageExisted(this,"com.instagram.android")) {
share.setPackage("com.instagram.android");
}
share.setType(type);
share.putExtra(Intent.EXTRA_STREAM, bmpUri);
startActivity(Intent.createChooser(share, "Share to"));
Try the following code:
File mFileImagePath = " /storage/emulated/0/Image Editor/Media/FilterImages/Image_Editor_1547816365839.jpg "; // Just example you use file URL
private boolean checkAppInstall(String uri) {
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
//Error
}
return false;
}
private void shareInstagram(File mFileImagePath) {
Intent intent = getPackageManager().getLaunchIntentForPackage("com.instagram.android");
if (intent != null) {
Intent mIntentShare = new Intent(Intent.ACTION_SEND);
String mStrExtension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(mFileImagePath).toString());
String mStrMimeType = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(mStrExtension);
if (mStrExtension.equalsIgnoreCase("") || mStrMimeType == null) {
// if there is no extension or there is no definite mimetype, still try to open the file
mIntentShare.setType("text*//*");
} else {
mIntentShare.setType(mStrMimeType);
}
mIntentShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mFileImagePath));
mIntentShare.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
mIntentShare.setPackage("com.instagram.android");
startActivity(mIntentShare);
} else {
Toast.makeText(mContext, "Instagram have not been installed.", Toast.LENGTH_SHORT).show();
}
}
This code works for me and will work on all Android devices.

Android, MediaStore.EXTRA_OUTPUT, stores two images [duplicate]

I'm currently developing an app which uses the built-in Camera.
I call this snippet by clicking a button :
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
//Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
path += "/myFolder/myPicture.jpg";
File file = new File( path );
//file.mkdirs();
Uri outputFileUri = Uri.fromFile( file );
//String absoluteOutputFileUri = file.getAbsolutePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 0);
After taking the picture with the camera, the jpg is well stored in sdcard/myFolder/myPicture.jpg, but it is also stored in /sdcard/DCIM/Camera/2011-06-14 10.36.10.jpg, which is the default path.
Is there a way to prevent the built-in Camera to store the picture in the default folder?
Edit : I think I will use the Camera class directly
Another way, tested on android 2.1, is take the ID or Absolute path of the gallery last image, then you can delete the duplicated image.
It can be done like that:
/**
* Gets the last image id from the media store
* #return
*/
private int getLastImageId(){
final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
final String imageOrderBy = MediaStore.Images.Media._ID+" DESC";
Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy);
if(imageCursor.moveToFirst()){
int id = imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID));
String fullPath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
Log.d(TAG, "getLastImageId::id " + id);
Log.d(TAG, "getLastImageId::path " + fullPath);
imageCursor.close();
return id;
}else{
return 0;
}
}
And to remove the file:
private void removeImage(int id) {
ContentResolver cr = getContentResolver();
cr.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media._ID + "=?", new String[]{ Long.toString(id) } );
}
This code was based on the post: Deleting a gallery image after camera intent photo taken
While the answer from "Ilango J" provides the basic idea.. I thought I'd actually write in how I actually did it.
The temporary file path that we were setting in intent.putExtra() should be avoided as it's a non standard way across different hardwares. On HTC Desire (Android 2.2) it did not work, And i've heard it works on other phones. It's best to have a neutral approach which works every where.
Please note that this solution (using the Intent) requires that the phone's SD Card is available and is not mounted onto the PC. Even the normal Camera app wouldn't work when the SD Card is connected to the PC.
1) Initiate the Camera Capture intent. Note, I disabled temporary file writes (non-standard across different hardware)
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(camera , 0);
2) Handle callback and retrieve the captured picture path from the Uri object and pass it to step#3
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CAPTURE_PIC: {
if (resultCode == RESULT_OK && data != null) {
Uri capturedImageUri = data.getData();
String capturedPicFilePath = getRealPathFromURI(capturedImageUri);
writeImageData(capturedImageUri, capturedPicFilePath);
break;
}
}
}
}
public String getRealPathFromURI(Uri contentUri) {
String[] projx = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, projx, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
3) Clone and delete the file. See that I used the Uri's InputStream to read the content.
The same can be read from the File of the capturedPicFilePath too.
public void writeImageData(Uri capturedPictureUri, String capturedPicFilePath) {
// Here's where the new file will be written
String newCapturedFileAbsolutePath = "something" + JPG;
// Here's how to get FileInputStream Directly.
try {
InputStream fileInputStream = getContentResolver().openInputStream(capturedPictureUri);
cloneFile(fileInputStream, newCapturedFileAbsolutePath);
} catch (FileNotFoundException e) {
// suppress and log that the image write has failed.
}
// Delete original file from Android's Gallery
File capturedFile = new File(capturedPicFilePath);
boolean isCapturedCameraGalleryFileDeleted = capturedFile.delete();
}
public static void cloneFile(InputStream currentFileInputStream, String newPath) {
FileOutputStream newFileStream = null;
try {
newFileStream = new FileOutputStream(newPath);
byte[] bytesArray = new byte[1024];
int length;
while ((length = currentFileInputStream.read(bytesArray)) > 0) {
newFileStream.write(bytesArray, 0, length);
}
newFileStream.flush();
} catch (Exception e) {
Log.e("Prog", "Exception while copying file " + currentFileInputStream + " to "
+ newPath, e);
} finally {
try {
if (currentFileInputStream != null) {
currentFileInputStream.close();
}
if (newFileStream != null) {
newFileStream.close();
}
} catch (IOException e) {
// Suppress file stream close
Log.e("Prog", "Exception occured while closing filestream ", e);
}
}
}
try this code:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
//Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
path += "/myFolder/myPicture.jpg";
File file = new File( path );
//file.mkdirs();
Uri outputFileUri = Uri.fromFile( file );
//String absoluteOutputFileUri = file.getAbsolutePath();
intent.putExtra("output", outputFileUri);
startActivityForResult(intent, 0);

Built-in Camera, using the extra MediaStore.EXTRA_OUTPUT stores pictures twice (in my folder, and in the default)

I'm currently developing an app which uses the built-in Camera.
I call this snippet by clicking a button :
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
//Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
path += "/myFolder/myPicture.jpg";
File file = new File( path );
//file.mkdirs();
Uri outputFileUri = Uri.fromFile( file );
//String absoluteOutputFileUri = file.getAbsolutePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 0);
After taking the picture with the camera, the jpg is well stored in sdcard/myFolder/myPicture.jpg, but it is also stored in /sdcard/DCIM/Camera/2011-06-14 10.36.10.jpg, which is the default path.
Is there a way to prevent the built-in Camera to store the picture in the default folder?
Edit : I think I will use the Camera class directly
Another way, tested on android 2.1, is take the ID or Absolute path of the gallery last image, then you can delete the duplicated image.
It can be done like that:
/**
* Gets the last image id from the media store
* #return
*/
private int getLastImageId(){
final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
final String imageOrderBy = MediaStore.Images.Media._ID+" DESC";
Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy);
if(imageCursor.moveToFirst()){
int id = imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID));
String fullPath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
Log.d(TAG, "getLastImageId::id " + id);
Log.d(TAG, "getLastImageId::path " + fullPath);
imageCursor.close();
return id;
}else{
return 0;
}
}
And to remove the file:
private void removeImage(int id) {
ContentResolver cr = getContentResolver();
cr.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media._ID + "=?", new String[]{ Long.toString(id) } );
}
This code was based on the post: Deleting a gallery image after camera intent photo taken
While the answer from "Ilango J" provides the basic idea.. I thought I'd actually write in how I actually did it.
The temporary file path that we were setting in intent.putExtra() should be avoided as it's a non standard way across different hardwares. On HTC Desire (Android 2.2) it did not work, And i've heard it works on other phones. It's best to have a neutral approach which works every where.
Please note that this solution (using the Intent) requires that the phone's SD Card is available and is not mounted onto the PC. Even the normal Camera app wouldn't work when the SD Card is connected to the PC.
1) Initiate the Camera Capture intent. Note, I disabled temporary file writes (non-standard across different hardware)
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(camera , 0);
2) Handle callback and retrieve the captured picture path from the Uri object and pass it to step#3
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CAPTURE_PIC: {
if (resultCode == RESULT_OK && data != null) {
Uri capturedImageUri = data.getData();
String capturedPicFilePath = getRealPathFromURI(capturedImageUri);
writeImageData(capturedImageUri, capturedPicFilePath);
break;
}
}
}
}
public String getRealPathFromURI(Uri contentUri) {
String[] projx = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, projx, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
3) Clone and delete the file. See that I used the Uri's InputStream to read the content.
The same can be read from the File of the capturedPicFilePath too.
public void writeImageData(Uri capturedPictureUri, String capturedPicFilePath) {
// Here's where the new file will be written
String newCapturedFileAbsolutePath = "something" + JPG;
// Here's how to get FileInputStream Directly.
try {
InputStream fileInputStream = getContentResolver().openInputStream(capturedPictureUri);
cloneFile(fileInputStream, newCapturedFileAbsolutePath);
} catch (FileNotFoundException e) {
// suppress and log that the image write has failed.
}
// Delete original file from Android's Gallery
File capturedFile = new File(capturedPicFilePath);
boolean isCapturedCameraGalleryFileDeleted = capturedFile.delete();
}
public static void cloneFile(InputStream currentFileInputStream, String newPath) {
FileOutputStream newFileStream = null;
try {
newFileStream = new FileOutputStream(newPath);
byte[] bytesArray = new byte[1024];
int length;
while ((length = currentFileInputStream.read(bytesArray)) > 0) {
newFileStream.write(bytesArray, 0, length);
}
newFileStream.flush();
} catch (Exception e) {
Log.e("Prog", "Exception while copying file " + currentFileInputStream + " to "
+ newPath, e);
} finally {
try {
if (currentFileInputStream != null) {
currentFileInputStream.close();
}
if (newFileStream != null) {
newFileStream.close();
}
} catch (IOException e) {
// Suppress file stream close
Log.e("Prog", "Exception occured while closing filestream ", e);
}
}
}
try this code:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
//Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
path += "/myFolder/myPicture.jpg";
File file = new File( path );
//file.mkdirs();
Uri outputFileUri = Uri.fromFile( file );
//String absoluteOutputFileUri = file.getAbsolutePath();
intent.putExtra("output", outputFileUri);
startActivityForResult(intent, 0);

Gallery with folder filter

I'm using following code to open a gallery inside of my app
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, FIND_RESULT);
Is it possible to limit a list of images to only show images taken by camera? Viewing Gallery on my 2.1 system, images are grouped so there has to be a parameter that defines to which folder it belongs.
Checking the MediaStore.Images.ImageColumns I did not a find any column that would define such thing.
Could I be wrong? Because if I could create a query to filter by folder and create my own gallery view, then my problem would be solved.
You just need to implement MediaScannerConnectionClient in your activity and after that you have to give the exact path of one of the file inside that folder name here as SCAN_PATH and it will scan all the files containing in that folder and open it inside built in gallery. So just give the name of you folder and you will get all the files inside including video. If you want to open only images change FILE_TYPE="image/*"
public class SlideShow extends Activity implements MediaScannerConnectionClient {
public String[] allFiles;
private String SCAN_PATH ;
private static final String FILE_TYPE = "*/*";
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/yourfoldername/");
allFiles = folder.list();
SCAN_PATH=Environment.getExternalStorageDirectory().toString()+"/yourfoldername/"+allFiles[0];
Button scanBtn = (Button) findViewById(R.id.scanBtn);
scanBtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
startScan();
}
});
}
private void startScan()
{
if(conn!=null)
{
conn.disconnect();
}
conn = new MediaScannerConnection(this, this);
conn.connect();
}
public void onMediaScannerConnected()
{
conn.scanFile(SCAN_PATH, FILE_TYPE);
}
public void onScanCompleted(String path, Uri uri)
{
try
{
if (uri != null)
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
}
}
finally
{
conn.disconnect();
conn = null;
}
}
}
None of the above answers are correct, including the one marked as correct.
Here's the actual correct solution:
The secret is finding the bucket/album your folder is represented as. Buckets show up after a successful MediaScan so be sure any images/videos you want to show are first scanned as demonstrated multiple times above.
Let's assume I have an indexed folder in /sdcard/myapp/myappsmediafolder:
String bucketId = "";
final String[] projection = new String[] {"DISTINCT " + MediaStore.Images.Media.BUCKET_DISPLAY_NAME + ", " + MediaStore.Images.Media.BUCKET_ID};
final Cursor cur = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
while (cur != null && cur.moveToNext()) {
final String bucketName = cur.getString((cur.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME)));
if (bucketName.equals("myappsmediafolder")) {
bucketId = cur.getString((cur.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_ID)));
break;
}
}
Now that we have the bucketId for our album we can open it with a simple intent.
Filters Video files:
Uri mediaUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
Filters Image files:
Uri mediaUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
...
if (bucketId.length() > 0) {
mediaUri = mediaUri.buildUpon()
.authority("media")
.appendQueryParameter("bucketId", bucketId)
.build();
}
Intent intent = new Intent(Intent.ACTION_VIEW, mediaUri);
startActivity(intent);
I can verify this works with the built-in Gallery app. Mileage may vary with other apps such as Google Photos.
I have yet to figure out how not to filter images/video, even though within Gallery you can select a specific Album with no filter.
I figured this out by looking at the AOSP source to the gallery app.
I don't have enough reputation to upvote or comment on his answer but ShellDude's answer allows you to put a directory URI in the gallery intent. So when the gallery app is opened it displays all of the images instead of 1.
For me, scanning my files like the answers above did not work. Querying the MediaStore.Images.Media.EXTERNAL_CONTENT_URI only worked after inserting new rows into the MediaStore.Images.Media.DATA table with the ContentResolver:
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, image.getPath());
values.put(MediaStore.Images.Media.MIME_TYPE,"image/jpeg");
contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Here is a simplified one
private MediaScannerConnection conn;
private void notifySystemWithImage(final File imageFile) {
conn = new MediaScannerConnection(this, new MediaScannerConnectionClient() {
#Override
public void onScanCompleted(String path, Uri uri) {
try {
if (uri != null) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
}
} finally {
conn.disconnect();
conn = null;
}
}
#Override
public void onMediaScannerConnected() {
conn.scanFile(imageFile.getAbsolutePath(), "*/*");
}
});
conn.connect();
}
For those who this still give activity not found exception:
You need to specify directory of your inner application folder. Not user default root if images and everything.
public class SlideShow extends Activity implements MediaScannerConnectionClient {
public String[] allFiles;
private String SCAN_PATH ;
private static final String FILE_TYPE = "*/*";
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(HistoryActivity.this.getExternalFilesDir(null)+"/a/");
allFiles = folder.list();
SCAN_PATH= HistoryActivity.this.getExternalFilesDir(null)+"/a/"+allFiles[0];
Button scanBtn = (Button) findViewById(R.id.scanBtn);
scanBtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
startScan();
}
});
}
private void startScan()
{
if(conn!=null)
{
conn.disconnect();
}
conn = new MediaScannerConnection(this, this);
conn.connect();
}
public void onMediaScannerConnected()
{
conn.scanFile(SCAN_PATH, FILE_TYPE);
}
public void onScanCompleted(String path, Uri uri)
{
try
{
if (uri != null)
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
}
}
finally
{
conn.disconnect();
conn = null;
}
}
}
works... but kitkat show only one photo. I managed to fix it for earlier versions with (updating gallery, when storing image):
public void savePhoto(Bitmap bmp)
{
File imageFileFolder = new File(context.getExternalFilesDir(null)+"/a/") ;
imageFileFolder.mkdir();
FileOutputStream out = null;
Calendar c = Calendar.getInstance();
String date = fromInt(c.get(Calendar.MONTH))
+ fromInt(c.get(Calendar.DAY_OF_MONTH))
+ fromInt(c.get(Calendar.YEAR))
+ fromInt(c.get(Calendar.HOUR_OF_DAY))
+ fromInt(c.get(Calendar.MINUTE))
+ fromInt(c.get(Calendar.SECOND));
File imageFileName = new File(imageFileFolder, date.toString() + ".jpg");
try
{
out = new FileOutputStream(imageFileName);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
scanPhoto(imageFileName.toString());
out = null;
} catch (Exception e)
{
e.printStackTrace();
}
}
public String fromInt(int val)
{
return String.valueOf(val);
}
public void scanPhoto(final String imageFileName)
{
msConn = new MediaScannerConnection(context,new MediaScannerConnection.MediaScannerConnectionClient()
{
public void onMediaScannerConnected()
{
msConn.scanFile(imageFileName, null);
Log.i("msClient obj in Photo Utility", "connection established");
}
public void onScanCompleted(String path, Uri uri)
{
msConn.disconnect();
Log.i("msClient obj in Photo Utility","scan completed");
}
});
msConn.connect();
}

Categories

Resources