Last active
April 26, 2024 07:40
-
-
Save nikartx/79932c0a4f0a644f7ce020143146db98 to your computer and use it in GitHub Desktop.
Android. Example how to save an image file in the App cache and get Uri for it. The Image will not be saved in a device gallery, only in an internal App cache.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.example; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.net.Uri; | |
import android.text.TextUtils; | |
import android.util.Log; | |
import androidx.annotation.Nullable; | |
import androidx.core.content.FileProvider; | |
import com.github.example.App; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
/** | |
* Save a file in the App cache and get Uri for it | |
* | |
* @author Ivan V on 12.05.2019. | |
* @version 1.0 | |
*/ | |
public class Cache { | |
public static final String TAG = Cache.class.getSimpleName(); | |
private static final String CHILD_DIR = "images"; | |
private static final String TEMP_FILE_NAME = "img"; | |
private static final String FILE_EXTENSION = ".png"; | |
private static final int COMPRESS_QUALITY = 100; | |
/** | |
* Save image to the App cache | |
* @param bitmap to save to the cache | |
* @param name file name in the cache. | |
* If name is null file will be named by default {@link #TEMP_FILE_NAME} | |
* @return file dir when file was saved | |
*/ | |
public File saveImgToCache(Bitmap bitmap, @Nullable String name) { | |
File cachePath = null; | |
String fileName = TEMP_FILE_NAME; | |
if (!TextUtils.isEmpty(name)) { | |
fileName = name; | |
} | |
try { | |
cachePath = new File(App.getAppContext().getCacheDir(), CHILD_DIR); | |
cachePath.mkdirs(); | |
FileOutputStream stream = new FileOutputStream(cachePath + "/" + fileName + FILE_EXTENSION); | |
bitmap.compress(Bitmap.CompressFormat.PNG, COMPRESS_QUALITY, stream); | |
stream.close(); | |
} catch (IOException e) { | |
Log.e(TAG, "saveImgToCache error: " + bitmap, e); | |
} | |
return cachePath; | |
} | |
/** | |
* Save an image to the App cache dir and return it {@link Uri} | |
* @param bitmap to save to the cache | |
*/ | |
public Uri saveToCacheAndGetUri(Bitmap bitmap) { | |
return saveToCacheAndGetUri(bitmap, null); | |
} | |
/** | |
* Save an image to the App cache dir and return it {@link Uri} | |
* @param bitmap to save to the cache | |
* @param name file name in the cache. | |
* If name is null file will be named by default {@link #TEMP_FILE_NAME} | |
*/ | |
public Uri saveToCacheAndGetUri(Bitmap bitmap, @Nullable String name) { | |
File file = saveImgToCache(bitmap, name); | |
return getImageUri(file, name); | |
} | |
/** | |
* Get a file {@link Uri} | |
* @param name of the file | |
* @return file Uri in the App cache or null if file wasn't found | |
*/ | |
@Nullable public Uri getUriByFileName(String name) { | |
Context context = App.getAppContext(); | |
String fileName; | |
if (!TextUtils.isEmpty(name)) { | |
fileName = name; | |
} else { | |
return null; | |
} | |
File imagePath = new File(context.getCacheDir(), CHILD_DIR); | |
File newFile = new File(imagePath, fileName + FILE_EXTENSION); | |
return FileProvider.getUriForFile(context, context.getPackageName() + ".provider", newFile); | |
} | |
// Get an image Uri by name without extension from a file dir | |
private Uri getImageUri(File fileDir, @Nullable String name) { | |
Context context = App.getAppContext(); | |
String fileName = TEMP_FILE_NAME; | |
if (!TextUtils.isEmpty(name)) { | |
fileName = name; | |
} | |
File newFile = new File(fileDir, fileName + FILE_EXTENSION); | |
return FileProvider.getUriForFile(context, context.getPackageName() + ".provider", newFile); | |
} | |
/** | |
* Get Uri type by {@link Uri} | |
*/ | |
public String getContentType(Uri uri) { | |
return App.getAppContext().getContentResolver().getType(uri); | |
} | |
} |
How to access Context from App?, it shows that context is private when I access it in cache.java
---SOLVED, I created a constructor and accessed context from there!
this is great stuff...!!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add the provider to manifest:
Create file_paths.xml in /res/xml/ with content:
Example of using: