Image Saving in Scoped Storage Android 10, not appearing in gallery - mediastore

Required Behaviour: I use following code to save images, and want to show them up in gallery(as we get to see images from whatsapp).
boolean downloadImage(Bitmap image) {
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(MainActivity.this, "Download started..", Toast.LENGTH_SHORT).show();
}
});
ContentResolver resolver = getApplicationContext().getContentResolver();
Uri imageCollection;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
imageCollection = MediaStore.Images.Media
.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
} else {
imageCollection = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}
String imgName = "IMG_"+System.currentTimeMillis()+".jpg";
ContentValues imgDetails = new ContentValues();
imgDetails.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
imgDetails.put(MediaStore.Images.Media.DISPLAY_NAME, imgName);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
imgDetails.put(MediaStore.Images.Media.IS_PENDING, 1);
imgDetails.put(MediaStore.Images.Media.RELATIVE_PATH, "DCIM/DownloaderApp");
}
Uri imgUri = resolver.insert(imageCollection, imgDetails);
try {
OutputStream outputStream = resolver.openOutputStream(imgUri);
boolean saveResult = image.compress(Bitmap.CompressFormat.JPEG, 90, outputStream);
outputStream.close();
imgDetails.clear();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
imgDetails.put(MediaStore.Images.Media.IS_PENDING, 0);
}
resolver.update(imageCollection, imgDetails, null, null);
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(
MainActivity.this,
(saveResult) ?"Image Path: "+imgUri.getPath() :"Failed to save image!",
Toast.LENGTH_SHORT).show();
}
});
outputStream.close();
return saveResult;
} catch(Exception e) {
e.printStackTrace();
return false;
}
}
Current Situation: I can see images getting saved in File Manager at exact location DCIM/DownloaderApp, but these pictures do not appear in gallery, i.e. they are not publically visibe through MediaStore API(what I feel).
Targetted Android: Android 10
I know, I can use android:requestLegacyExternalStorage="true" in manifest. But I want the app to be perfectly compatible with android 11 as well. So don't want to do this.

imageCollection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
change VOLUME_EXTERNAL_PRIMARY to VOLUME_EXTERNAL
and lastly in
contentResolver.update(imageCollection,contentValues,null,null)
make little modification
contentResolver.update(imageUri,contentValues,null,null)
Your case contentValues name will be imageDetails.
you can see your photos on the gallery.

Related

Camera cannot take image when app go background but work fine when app in foreground

I am making application which take image from front camera on firebase remote command. App work fine and take picture without user interaction, but when app close or go to foreground, app start giving error that Fail to connect to camera service. As soon as app open it capture the image.
I run foreground service notification which working but still same camera fail error and can not take picture.
try {
Log.d("kkkk", "Preparing to take photo");
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
int frontCamera = cam;
//int backCamera=0;
Camera.getCameraInfo(frontCamera, cameraInfo);
try {
camera = Camera.open(frontCamera);
} catch (RuntimeException e) {
Log.d("kkkk", "Camera not available: " + e.getMessage());
camera = null;
// takePicture(0);
}
try {
if (null == camera) {
Log.d("kkkk", "Could not get camera instance");
} else {
Log.d("kkkk", "Got the camera, creating the dummy surface texture");
try {
camera.setPreviewTexture(new SurfaceTexture(0));
camera.startPreview();
} catch (Exception e) {
Log.d("kkkk", "Could not set the surface preview texture");
e.printStackTrace();
}
camera.takePicture(null, null, new Camera.PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.d("kkkk", "clicked");
// Encode the byte array into a base64 string
// String imageString = android.util.Base64.encodeToString(imageBytes, android.util.Base64.DEFAULT);
// Log.d("error200", imageString);
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReference();
String path = "images/"+username.toLowerCase()+device.replace(" ","");
StorageReference imageRef = storageRef.child(path);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); // Replace this with your bitmap image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 10, baos);
byte[] data0 = baos.toByteArray();
UploadTask uploadTask = imageRef.putBytes(data0);
uploadTask.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle unsuccessful uploads
Log.d("pic","fail"+exception.getMessage());
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Handle successful uploads
Log.d("pic","done");
}
});
camera.release();
}
});
}
} catch (Exception e) {
camera.release();
}
} catch (Exception e) {
Log.d("errorData", e.getMessage());
}
onDestroy method I release the camera but still same error.
#Override
public void onDestroy() {
super.onDestroy();
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
}```
Can you add START_ACTIVITIES_FROM_BACKGROUND permission in the manifest if you have not added then you need to add this permission for using a camera in the foreground service. But I'm not sure the android newer version supports the used camera without user interaction
Please Refer Official Android Documentation here

Bitmap compress quality ok in emulator, but not in real device

In my app, images are saving successfully to gallery after editing. but quality is not up to the mark on physical device. I got 0.5mp to 0.7mp highest. but same app I open in emulator and after saving image I got pretty good quality of images (about 1.5mp to 3mp). didn't find the exact reason of this. will be glad if you help to find out. attaching my image saving code below.
public void saveAsFile(#NonNull final String str, #NonNull final SaveSettings saveSettings, #NonNull final OnSaveListener onSaveListener) {
Log.d(TAG, "Image Path: " + str);
this.parentView.saveFilter((OnSaveBitmap) new OnSaveBitmap() {
#Override
public void onBitmapReady(Bitmap bitmap) {
new AsyncTask<String, String, Exception>() {
#Override
public void onPreExecute() {
super.onPreExecute();
PhotoEditor.this.clearHelperBox();
PhotoEditor.this.parentView.setDrawingCacheEnabled(false);
}
#SuppressLint({"MissingPermission"})
public Exception doInBackground(String... strArr) {
Bitmap bitmap;
try {
FileOutputStream fileOutputStream = new FileOutputStream(new File(str), false);
if (PhotoEditor.this.parentView != null) {
PhotoEditor.this.parentView.setDrawingCacheEnabled(true);
if (saveSettings.isTransparencyEnabled()) {
bitmap = BitmapUtil.removeTransparency(PhotoEditor.this.parentView.getDrawingCache());
} else {
bitmap = PhotoEditor.this.parentView.getDrawingCache();
}
bitmap.compress(Bitmap.CompressFormat.PNG, 100 , fileOutputStream);
}
Log.d(PhotoEditor.TAG, "Filed Saved Successfully");
return null;
} catch (Exception e) {
e.printStackTrace();
Log.d(PhotoEditor.TAG, "Failed to save File");
return e;
}
}
#Override
public void onPostExecute(Exception exc) {
super.onPostExecute(exc);
if (exc == null) {
if (saveSettings.isClearViewsEnabled()) {
PhotoEditor.this.clearAllViews();
}
onSaveListener.onSuccess(str);
return;
}
onSaveListener.onFailure(exc);
}
}.execute();
}
public void onFailure(Exception exc) {
onSaveListener.onFailure(exc);
}
});
}
I tried in many ways but couldn't find the solution.

I can't show up the captured image in the thumbnails

its a class assignment and I need to finish it today.
So basically the captured image is saved but it won't show up in the imgVpic imageview camera open up and the image captured is saved correctly with the correct file name but it just won't show up in the thumbnail that I set up, I checked in the gallery and the picture is there but again .. the image won't show in this imgVpic image view (i kept repeating this cause I can't post this question without "more details" since my post is mostly code, pls help I'm a student and I hate my teacher)
here is the code that I used
public class CaptureImgActivity extends AppCompatActivity {
final int CAMERA_RESULT=0;
Uri imgUri=null;
Button btTakePic;
ImageView imgVPic;
private void CaptureImageUri(){
String dir= Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM).getAbsolutePath();
File folder=new File(dir + File.separator+"test");
boolean success=true;
if (!folder.exists()){
try {
success=folder.mkdir();
Toast.makeText(this,"Folder Create Successfull", Toast.LENGTH_SHORT).show();
}catch (Exception e){
success = !success;
e.printStackTrace();
}
}
if (success){
dir=dir+ File.separator+"test";
}
Calendar calendar=Calendar.getInstance();
File file= new File(dir,"test"+(calendar.getTimeInMillis()+".jpg"));
if (!file.exists()){
try {
file.createNewFile();
imgUri= FileProvider.getUriForFile(CaptureImgActivity.this,BuildConfig.APPLICATION_ID,file);
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,imgUri);
startActivityForResult(intent,CAMERA_RESULT);
}catch (Exception e){
e.printStackTrace();
}
}else {
try {
file.delete();
file.createNewFile();
imgUri= FileProvider.getUriForFile(CaptureImgActivity.this,BuildConfig.APPLICATION_ID,file);
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,imgUri);
startActivityForResult(intent,CAMERA_RESULT);
}catch (Exception e){
e.printStackTrace();
}
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if (requestCode == CAMERA_RESULT && requestCode == RESULT_OK){
try {
Uri imgUri=data.getData();
Bitmap bitmap= MediaStore.Images.Media.getBitmap(this.getContentResolver(),imgUri);
imgVPic.setImageBitmap(bitmap);
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_capture_img);
btTakePic = (Button) findViewById(R.id.btTakePic);
imgVPic = (ImageView) findViewById(R.id.imgVPic);
btTakePic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CaptureImageUri();
}
});
}
}
I tried in both method like choose image from gallery and Capture Image by adding
android:requestLegacyExternalStorage="true"
My Code is Different from yours but work as the same process but this works for me
Uri imgUri=null;
protected static final int CAMERA_REQUEST = 0;
protected static final int GALLERY_PICTURE = 1;
private Intent pictureActionIntent = null;
Bitmap bitmap;
String selectedImagePath;
private void SelectImage() {
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(
getActivity());
myAlertDialog.setTitle("Upload Pictures Option");
myAlertDialog.setMessage("How do you want to set your picture?");
myAlertDialog.setPositiveButton("Gallery",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Intent pictureActionIntent = null;
pictureActionIntent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(
pictureActionIntent,
GALLERY_PICTURE);
}
});
myAlertDialog.setNegativeButton("Camera",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
imgUri= FileProvider.getUriForFile(Objects.requireNonNull(getContext()), BuildConfig.APPLICATION_ID + ".provider", file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
startActivityForResult(intent, CAMERA_REQUEST);
}
});
myAlertDialog.show();
}
OnActivityResult
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
bitmap = null;
selectedImagePath = null;
if (resultCode == RESULT_OK && requestCode == CAMERA_REQUEST) {
File f = new File(Environment.getExternalStorageDirectory()
.toString());
for (File temp : f.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
f = temp;
break;
}
}
if (!f.exists()) {
Toast.makeText(getContext(), "Error while capturing image", Toast.LENGTH_LONG).show();
return;
}
try {
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
bitmap = Bitmap.createScaledBitmap(bitmap, 400, 400, true);
int rotate = 0;
try {
ExifInterface exif = new ExifInterface(f.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
mImageView.setImageBitmap(bitmap);
//storeImageTosdCard(bitmap);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (resultCode == RESULT_OK && requestCode == GALLERY_PICTURE) {
if (data != null) {
Uri selectedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor c = getActivity().getContentResolver().query(selectedImage, filePath,
null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
selectedImagePath = c.getString(columnIndex);
c.close();
bitmap = BitmapFactory.decodeFile(selectedImagePath); // load
mImageView.setImageBitmap(bitmap);
} else {
Toast.makeText(getContext(), "Cancelled",
Toast.LENGTH_SHORT).show();
}
}
}
And both method works for me and displayed thumbnail

Download File using download manager and save file based on click

I have my download manager, and it work perfect if I try to download a file. But I have a problem.
I have 4 CardView in my activity and I set it onClickListener, so when I click one CardView it will download the file.
Here is the code to call the download function
cardviewR1 = findViewById(R.id.card_viewR1);
cardviewR1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pDialogDL = new ProgressDialog(this);
pDialogDL.setMessage("A message");
pDialogDL.setIndeterminate(true);
pDialogDL.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialogDL.setCancelable(true);
final DownloadTask downloadTask = new DownloadTask(this);
downloadTask.execute(R1Holder);
pDialogDL.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
downloadTask.cancel(true);
}
});
}
});
and here is the download function
private class DownloadTask extends AsyncTask<String, Integer, String> {
private Context context;
private PowerManager.WakeLock mWakeLock;
public DownloadTask(Context context) {
this.context = context;
}
#Override
protected String doInBackground(String... sUrl) {
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL(sUrl[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
}
// this will be useful to display download percentage
// might be -1: server did not report the length
int fileLength = connection.getContentLength();
// download the file
input = connection.getInputStream();
output = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/"+getString(R.string.r1)+"_"+NameHolder+".zip");
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
// allow canceling with back button
if (isCancelled()) {
input.close();
return null;
}
total += count;
// publishing the progress....
if (fileLength > 0) // only if total length is known
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
} catch (Exception e) {
return e.toString();
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
} catch (IOException ignored) {
}
if (connection != null)
connection.disconnect();
}
return null;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
// take CPU lock to prevent CPU from going off if the user
// presses the power button during download
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
getClass().getName());
mWakeLock.acquire();
pDialogDL.show();
}
#Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
// if we get here, length is known, now set indeterminate to false
pDialogDL.setIndeterminate(false);
pDialogDL.setMax(100);
pDialogDL.setProgress(progress[0]);
}
#Override
protected void onPostExecute(String result) {
mWakeLock.release();
pDialogDL.dismiss();
if (result != null)
Toast.makeText(context, "Download error: " + result, Toast.LENGTH_LONG).show();
else
Toast.makeText(context, "File downloaded", Toast.LENGTH_SHORT).show();
}
}
The code work in my app, but the problem is, when I try to add second CardView which is like this
cardviewR2 = findViewById(R.id.card_viewR2);
cardviewR2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pDialogDL = new ProgressDialog(this);
pDialogDL.setMessage("A message");
pDialogDL.setIndeterminate(true);
pDialogDL.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialogDL.setCancelable(true);
final DownloadTask downloadTask = new DownloadTask(this);
downloadTask.execute(R2Holder);
pDialogDL.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
downloadTask.cancel(true);
}
});
}
});
Yes it will download the second file, but it will overwrite the first file. I think the problem is right here
output = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/"+getString(R.string.r1)+"_"+NameHolder+".zip");
Anyone can help me with this code?
I need your help, Thanks
Fixed it by create a new Download Class separately in different file with activity, so the AsyncTask will be call again and again
thanks

How to work with LWUIT TABs click events

UPDATE:
My Requirement is to display two Rss files as Tabs on my LWUIT Form
Initially by default first Rss file titles and images should be displayed on first tab
if an end user click on second tab,we should be able to load the second rss file titles and images
I am able to load first Rss File titles,but i am not able to load the second tab if i click on it
How to capture the click event for LWUIT Tab?
Here my code which is not working:
String topNewsurl="TopNews.rss";
String topStoryurl="TopStory.rss";
public class XMLMidlet extends MIDlet{
public void startApp() {
Display.init(this);
Process p;
try {
p = new Process(this);
p.process();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public class Process extends Form {
Process(XMLMidlet midlet) throws IOException {
this.midlet=midlet;
topnews = new Vector();
topstory = new Vector();
tabs = new Tabs();
form1 = new Form();
form2=new Form();
form1.setLayout(new BorderLayout());
form1.setScrollable(false);
image = Image.createImage("/res/Tone.jpg");
Label icon = new Label(image);
form1.setTitleComponent(icon);
form2.setTitleComponent(icon);
form1.setTransitionInAnimator(Transition3D.createRotation(250, true));
try {
newsList = new List(topnews);
newsList.setScrollVisible(false);
newsList.setRenderer(new NewsListCellRenderer());
myNewsList = new List(topstory);
myNewsList.setScrollVisible(false);
myNewsList.setRenderer(new NewsListCellRenderer());
tabs.addTab("Topstory", newsList);
tabs.addTab("TopNews", myNewsList);
tabs.setChangeTabOnFocus(true);
form1.addComponent(BorderLayout.CENTER, tabs);
}
try{
String url = "http:topnews-20.rss";
form1.show();
ParseThread myThread = new ParseThread(this);
myThread.getXMLFeed(url);
} catch (Exception e) {
e.printStackTrace();
}
}
public void addNews(News newsItem) {
//log.debug("addnews");
//System.out.println("addNews");
topnews.addElement(newsItem);
newsList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
List source = (List) ae.getSource();
News selectedNewsItem = (News) source.getSelectedItem();
if (selectedNewsItem != null) {
displayCompleteNewsScreen(selectedNewsItem);
}
}
});
form1.show();
}
public void keyReleased(int keyCode) {
System.out.println("str");
Component p=this.getFocused();
String str= p.getClass().getName();
if(str.toLowerCase().indexOf("radiobutton")!=-1){
process();
}
From the very vague question it seems you want to capture key presses on a LWUIT Form.
jobsForm.addGameKeyListener(Display.GAME_FIRE,
new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//do something here
}
});
jobsForm.addPointerPressedListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
pointer_click = true;
}
});
jobsForm.addPointerReleasedListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (pointer_click) {
//
}
pointer_click = false;
}
});
jobsForm.addPointerDraggedListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//System.out.println("POINTER DRAGGED");
pointer_click = false;
}
});

Resources