How to save picture in memory (not in internal storage) - android

Please Look at the codes below.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, PICK_FROM_CAMERA);
This code is for calling camera function.
After I take a picture, the method onActivityResult gives me back Uri so that I can get the absolute path to internal storage of the picture. But, I want to use the picture just for temporary use. that is, I don't want the picture to be stored in internal storage. I need absolute path to make the picture into a File object. So, is there any way to store the picture on memory allocated to my app??
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(resultCode != RESULT_OK)
return;
if(requestCode == PICK_FROM_CAMERA){
imageUri = data.getData();
Log.d("메시지", "uri = "+imageUri);
Cursor c = this.getContentResolver().query(imageUri, null, null, null, null);
c.moveToNext();
absolutePath = c.getString(c.getColumnIndex(MediaStore.MediaColumns.DATA));
}
}

For this you can use Cache Directory:
File storagePath = new File(getCacheDir() + "/" + "YOUR_FILE_NAME.JPG");

Related

Opening large SQLite Databases on Android 11

I need to open a large SQLite Database on Android 11 (api level 30). The Documentation says that "MANAGE_EXTERNAL_STORAGE" is a Problem in the Future.
Therefore, I read the Docs at: https://developer.android.com/training/data-storage/shared/documents-files and used:
public void openDirectory(Uri uriToLoad) {
// Choose a directory using the system's file picker.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, 42);
}
#Override
public void onActivityResult(int requestCode, int resultCode,
Intent resultData) {
if (requestCode == 42
&& resultCode == Activity.RESULT_OK) {
// The result data contains a URI for the document or directory that
// the user selected.
Uri uri = null;
if (resultData != null) {
uri = resultData.getData();
DocumentFile dfile = DocumentFile.fromTreeUri(this, uri);
DocumentFile[] fileList = dfile.listFiles();
}
}
}
If I understand the Documentation right, File is deprecated or can not be used with Files on the SD Card and DocumentFile/URI is the new thing. But to open the Database I use:
SQLiteDatabase.openDatabase(
pFile.getAbsolutePath(),
null,
SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY))
So my Problem is: How to open a large (10Gb or more) Database with an URI or a DocumentFile.
Ps.: The Database is a RasterLite File with map images

Get bitmap from camera android

I'm trying to open the camera from my app and get a bitmap. But this doens't work. I got this error :
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: content:/media/external/images/media/9969: open failed: ENOENT (No such file or directory)
Here is the code :
private void openCamera()
{
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Images.Media.TITLE, "New Picture");
contentValues.put(MediaStore.Images.Media.DESCRIPTION, "From the camera");
image_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
//Camera intent
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri);
startActivityForResult(cameraIntent, IMAGE_CAPTURE_CODE);
}
When I call openCamera(), here is the activity result :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (resultCode)
{
case RESULT_OK :
{
//imageView.setImageURI(image_uri); this works
Bitmap bitmap = null;
String photoPath = image_uri.toString();
bitmap = BitmapFactory.decodeFile(photoPath);
imageView.setImageBitmap(bitmap); //this doesnt work ! --> the bitmap is empty
}
//finish
case RESULT_CANCELED :
{
// finish();
}
default: {
//finish();
}
}
I need the bitmap to store the image after that.
It's pretty weird because the image is well saved into the gallery but I can't get it...
Ideally, you would be using Glide, Picasso, or another image-loading library, and simply have it load the image from the Uri into your ImageView.
If you insist upon using your basic approach, despite its poor performance, replace:
String photoPath = image_uri.toString();
bitmap = BitmapFactory.decodeFile(photoPath);
with:
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(image_uri));
A Uri is not a file.

How to convert content:// to file after image capture android?

I am capturing image using camera in android using the following code snippet.
Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(Constants.ATTACH_IMAGE);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
imageIntent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString());startActivityForResult(imageIntent, Constants.ATTACH_IMAGE);
in onActivityResult, I want to convert the "content://" uri back to File, compress it and show a thumb nail.
How can I do it?
Edit :-
What I have tried:-
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("activity result","coming" + String.valueOf(requestCode)+ contentFileUri);
// View popupView = getActivity().getLayoutInflater().inflate(R.layout.popup_add_snag, null);
// LinearLayout attachLayout = (LinearLayout)popupView.findViewById(R.id.attachLayout);
// ImageView photoImageicon = (ImageView)popupView.findViewById(R.id.imageicon);
try{
// if (resultCode == Constants.RESULT_OK){
if(contentFileUri !=null) {
Log.d("fileuri", contentFileUri.toString());
String attachmentType = "IMAGE";
// mAdapter.attachmentType=attachmentType;
photoImageIcon.setVisibility(View.VISIBLE);
// attachLayout.setVisibility(View.INVISIBLE);
Toast.makeText(getActivity().getApplicationContext(), R.string.successfull_image, Toast.LENGTH_SHORT).show();
Log.d("fileuri", contentFileUri.toString());
InputStream inputStream = getContext().getContentResolver().openInputStream(contentFileUri);
contentFileUri looks like this:- "content://com.fa.ha.provider/external_files/F/7c7.jpeg"
But above results in "FileNotFound Exception, not a directory" at the last line.
Ideally, just hang onto the File that getOutputMediaFileUri() is creating. Hopefully, getOutputMediaFileUri() is setting up a FileProvider Uri for you, based off of a File, so if you hold onto the File, you do not need to worry about "converting" anything. See this sample app.
If getOutputMediaFileUri() is poorly written, perhaps you do not have a File. In that case, in onActivityResult(), you can pass the Uri to an image-loading library (Picasso, Glide, etc.) and have it populate an ImageView or simply load the Bitmap for you.

IMAGE_CAPTURE on resultActivity the file doesn't exist yet

This a rare case, i'm trying to capture an image from native camera activity on my samsung galaxy s3 and onActivityResult the file doesn't exist to read yet. If i disconnect the phone and connect again then the i can see the image on my explorer.
It likes that the camera activity writes the file with delay or something...
i have readed all questions and answers and other forums, but it is my problem; My code:
... onCreate
File file = new File( _path );
pathUri = Uri.fromFile( file );
...
protected void startCameraActivity()
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, pathUri);
startActivityForResult( intent, CAMERA_REQUEST_CODE );
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK)
{
// test if exits image
if((new File(_path)).exists())
... do something
}
... continue
if((new File(_path)).exists()) always retunrs FALSE, how is it possible?
Thans in advance
I have never seen this occur in my app. However, this could potentially be a workaround.
Note: Declare the Uri instance initialURI globally so you can use it throughout the Activity. You could also change the name to something that suits your naming convention.
In this, right before the Intent is triggered, I pass a specific path for the Image to be stored at along with the name for the File.
Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");
File cameraFolder;
if (android.os.Environment.getExternalStorageState().equals
(android.os.Environment.MEDIA_MOUNTED))
cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),
"SOME_FOLDER_NAME/");
else
cameraFolder= StatusUpdate.this.getCacheDir();
if(!cameraFolder.exists())
cameraFolder.mkdirs();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
String timeStamp = dateFormat.format(new Date());
String imageFileName = "picture_" + timeStamp + ".jpg";
File photo = new File(Environment.getExternalStorageDirectory(),
"SOME_FOLDER_NAME/" + imageFileName);
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
initialURI = Uri.fromFile(photo);
startActivityForResult(getCameraImage, ACTION_REQUEST_CAMERA);
And finally, in the onActivityResult():
Note: This I'm guessing should work. My application uses the Aviary SDK and the code for that differs vastly from the conventional work done in this method.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
// USE EITHER THIS
imgView.setImageURI(initialURI);
// OR USE THIS
getContentResolver().notifyChange(initialURI, null);
ContentResolver cr = getContentResolver();
try {
// SET THE IMAGE FROM THE CAMERA TO THE IMAGEVIEW
Bitmap bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, initialURI);
// SET THE IMAGE USING THE BITMAP
imgvwSelectedImage.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
If you do not need the the Image stored on the SD-Card, you could delete it once you are done with it. This I am guessing (guessing because I have never experienced what the OP is facing problem with) this should do it for you.
Oh. Almost forgot. You will need to declare this permission in your Manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Getting images from system Gallery. Why is not working?

I made a program where user show which picture he want to load in this app. When user want to load picture, he can choose what kind of file explorer he want to use:
Everything works, but if I choose "Gallery" and mark my image, it doesn't work. It happens only when i choose "Gallery".
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
String path = data.getData().getPath();
try {
loadedimage = BitmapFactory.decodeFile(sciezka_z_obrazem);
}
catch (Exception e) {
}
} [...]
After picking image from Gallery, loadedimage is null.
When I use Dropbox or "My Files" everything is OK.
I guess you should use
Uri mUri = data.getData();
In case of Gallery the file returned is of the form content:// rather than file://
So you should use the uri rather than doing getPath on uri
Edit:
Use uri to decode like below
InputStream is = getContentResolver().openInputStream(uri);
loadedimage = BitmapFactory.decodeStream(is)
You should try this.
File filePath= new File(path);
and then use
Uri.fromFile(filePath)
this would set the content wchich could be openend.

Categories

Resources