Can someone tell me why this code is not saving the picture to the gallery anymore? I had it working at some point, then i changed something somewhere else, and now this doesn't work.
private void openImageIntent() {
// Determine Uri of camera image to save.
final File storageDir = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
final String fname = ClassName.getUniqueImageFilename();
final File sdImageMainDirectory = new File(storageDir, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(
captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName,
res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent,
"Chose a source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
cameraIntents.toArray(new Parcelable[] {}));
startActivityForResult(chooserIntent, REQUEST_IMAGE_CAPTURE);
}
private static String getUniqueImageFilename() {
// TODO Auto-generated method stub
String fileName = "img_" + System.currentTimeMillis() + ".jpg";
return fileName;
}
This is the error i get:
10-29 23:53:50.692: E/BitmapFactory(12547): Unable to decode stream: java.io.FileNotFoundException: /file:/storage/emulated/0/DCIM/img_1414623222659.jpg: open failed: ENOENT (No such file or directory)
10-29 23:53:50.692: E/BitmapFactory(12547): Unable to decode stream: java.io.FileNotFoundException: /file:/storage/emulated/0/DCIM/img_1414623222659.jpg: open failed: ENOENT (No such file or directory)
final File storageDir = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
You should put permission in Android Manifest to write to External Storage
Android storage is broken over different version of Android SDK. Your code will work on some devices but not all.
The System cannot create a file on system because of lack of permission so it is giving you error. The best way I have found is just to see that if external storage is mounted then create a file on external.
There are following two code snippets and they both will work.
Environment.getExternalStorageDirectory() + "/" + "myImages" + "/someimage.jpg";
After that you can start Media Store update Intent to update Android Gallery.
or Use this code
final File dir = new File(context.getFilesDir() + "/nfs/guille/groce/users/nicholsk/workspace3/SQLTest");
dir.mkdirs(); //create folders where write files
final File file = new File(dir, "BlockForTest.txt");
Related
I tried to upload text file in my phone(Android 11) to Gmail, Dropbox, Google Drive...
But, all failed.
I wrote android:preserveLegacyExternalStorage="true" tag in the manifest file.
TargetSdkVersion is 29.
Download file from above apps works fine. Only upload does not work.
It works for my old phone.(below Android 10)
I think, problem is caused by "Android 11 open failed: EACCES (Permission denied)"
This is my code. How can I solve this problem?
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED))
path = Environment.getExternalStorageDirectory().getAbsolutePath();
else
path = Environment.MEDIA_UNMOUNTED;
path = path + "/myFolder"; // /storage/emulated/0/myFolder
File dir = new File(path);
if (!dir.exists())
dir.mkdir();
..........................................................
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
File file = new File(myFolder + "/" + "test.txt");
Uri uri = Uri.fromFile(file);
intent.putExtra(Intent.EXTRA_STREAM, uri);
Intent chooser = Intent.createChooser(intent, null);
if (intent.resolveActivity(getPackageManager()) != null)
startActivity(chooser);
I have built an Camera application following a Tutorial from Youtube. It saves the Files on External Storage with following code
public void takePicture(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = Uri.fromFile(getOutputMediaFile());
intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
startActivityForResult(intent, 100);
}
private static File getOutputMediaFile() {
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "CameraDemo");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("CameraDemo", "failed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
return new File(mediaStorageDir.getPath() + File.separator +
"IMG_" + timeStamp + ".jpg");
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100) {
if (resultCode == RESULT_OK) {
imageView.setImageURI(file);
}
}
}
But, I would like to show a Dialog box to user when app starts with directory chooser dialog where they want to save images which I followed the Tutorial from this link https://www.codeproject.com/Articles/547636/Android-Ready-to-use-simple-directory-chooser-dial
The code for Directory choose dialog is below
DirectoryChooserDialog directoryChooserDialog =
new DirectoryChooserDialog(MainActivity.this,
new DirectoryChooserDialog.ChosenDirectoryListener()
{
#Override
public void onChosenDir(String chosenDir)
{
m_chosenDir = chosenDir;
Toast.makeText(
MainActivity.this, "Chosen directory: " +
chosenDir, Toast.LENGTH_LONG).show();
}
});
// Toggle new folder button enabling
directoryChooserDialog.setNewFolderEnabled(m_newFolderEnabled);
// Load directory chooser dialog for initial 'm_chosenDir' directory.
// The registered callback will be called upon final directory selection.
directoryChooserDialog.chooseDirectory(m_chosenDir);
m_newFolderEnabled = ! m_newFolderEnabled;
And also indeed, I have a separate class of DirectoryChooserDialog.
Now how do I merge the first code with second one such that when Image taken from camera will save to the folder that users selected from second code not to external storage Directory as specified by first code.
Image file for DirectoryChooserDialog appears as
I am trying to share the .xlsx file stored in App's internal storage.
when i tried below code it shows unable to load reasource.
public void share(String fileName, Context context)
{
Uri fileUri = Uri.parse(Environment.getDataDirectory() + getPackageName() + "/files/" + fileName);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
shareIntent.setType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
startActivity(Intent.createChooser(shareIntent, "Send to"));
}
any help will be appreciable
How to open inbuilt gallery app with a specific album opened on screen ?
I know this code can be used for launching gallery app , but how to filter a specific album ? or open it ?
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
Assume your photos ares stored at youCam folder or it can be any folder
If you are compiling against API 23 or Greater then get runtime permision READ_EXTERNAL_STORAGE
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/youCam);
File[] listFile = file.listFiles();
new HomeScreen.SingleMediaScanner(HomeScreen.this, listFile[0]);
...
public class SingleMediaScanner implements MediaScannerConnection.MediaScannerConnectionClient {
private MediaScannerConnection mMs;
private File mFile;
public SingleMediaScanner(Context context, File f) {
mFile = f;
mMs = new MediaScannerConnection(context, this);
mMs.connect();
}
public void onMediaScannerConnected() {
mMs.scanFile(mFile.getAbsolutePath(), null);
}
public void onScanCompleted(String path, Uri uri) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
mMs.disconnect();
}
}
This will open you all images in that folder in default gallery.
Hi when I do something like this
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
In onActivityResult I get thumbnail of the image from the data intent from the camera app
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
And I use that bitmap something like this.
But What if I want the imageuri so that I can get the full size image from it. I tried getting the image uri from the intent something like above
Uri uri = data.getData();
if (uri != null) {
Log.d(TAG, uri.toString());
}else{
Log.d(TAG,"uri is null");
}
Doing like this I get uri is null in my logcat.So can anyone let me know how to get the image uri.I dont want to use EXTRA_OUTPUT and specify my own path.Thanks in advance
There is a well documented bug, which occurs in low resolution devices. Check this thread for the workaround.
There's a bug with that intent in some devices. Take a look at this to know how to workaround it.
In some devices, the Uri is null in onActivityForResult(). So you need
to set Uri to placing the captured image.
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// If there any applications that can handle this intent then call the intent.
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
Uri fileUri = Uri.fromFile(getOutputMediaFile());
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(takePictureIntent, CAMERA_PICKER);
}
public File getOutputMediaFile() {
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir;
// If the external directory is writable then then return the External pictures directory.
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp");
} else {
mediaStorageDir = Environment.getDownloadCacheDirectory();
}
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
return mediaFile;
}