I am creating an app that is downloading a file then loads it onto an imageview but I am getting this error
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
and this is my download code
class DownloadFileFromURL extends AsyncTask<String, String, String> {
// Show Progress bar before downloading Music
#Override
protected void onPreExecute() {
super.onPreExecute();
// Shows Progress Bar Dialog and then call doInBackground method
showDialog(progress_bar_type);
}
// Download File from Internet
#Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// Get Music file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(),10*1024);
// Output stream to write file in SD card
int imageNr = sharedPreferences.getInt("ImageNr", 1);
OutputStream output = new FileOutputStream(getApplicationInfo().dataDir+"/files/"+imageNr+".jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
total += count;
// Publish the progress which triggers onProgressUpdate method
publishProgress("" + (total));
// Write data to file
}
// Flush output
output.flush();
// Close streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
// While Downloading Music File
protected void onProgressUpdate(String... progress) {
// Set progress percentage
prgDialog.setProgress(Integer.parseInt(progress[0]));
}
// Once File is downloaded
#Override
protected void onPostExecute(String file_url) {
// Dismiss the dialog after the Music file was downloaded
dismissDialog(progress_bar_type);
int imageNr = sharedPreferences.getInt("ImageNr", 1);
File imgFile = new File(getApplicationInfo().dataDir+"/files/"+imageNr+".jpg");
imageNr++;
SharedPreferences.Editor editorsave = sharedPreferences.edit();
editorsave.putInt("ImageNr", imageNr);
editorsave.apply();
if(imgFile.exists()){
Toast.makeText(getApplicationContext(), "Bild laddad!", Toast.LENGTH_LONG).show();
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imageView.setImageBitmap(myBitmap);
}else{
Toast.makeText(getApplicationContext(), "Bild ej hittad!", Toast.LENGTH_LONG).show();
}
// Do stuff here
}
}
yeah it uses some deprecated code but this is working for me in my other application, though I am not using bitmap in that one but the AsyncTask works.
your imageViewis null
to stay way from the exception itself
if(imgFile.exists() && imageView != null) {
....
check your imageView reference assignment. It seems imageView reference is not assigned properly, hence returns null
Related
I'm having a very weird problem with AsyncTask which I use to download a zip file in my android application. It was working flawlessly until I decided to use strings.xml resource for every string linked to this task.
when I click on the download button inside my app, the progressbar of the AsyncTask shows for a second or less then dismisses itself and the task goes to the onPostExecute() state.
I tried debuging the app on my test device and there is no error about the task. I even added some stubs with Log.d tag, I've included the logcat results:
275-15524/xmc.androidexpert35.com.xtrememusicchecker D/ANDRO_ASYNC: path set
2019-04-04 20:19:22.484 15275-15524/xmc.androidexpert35.com.xtrememusicchecker D/ANDRO_ASYNC: Try block
2019-04-04 20:19:22.487 15275-15524/xmc.androidexpert35.com.xtrememusicchecker D/ANDRO_ASYNC: file url got
2019-04-04 20:19:22.490 15275-15524/xmc.androidexpert35.com.xtrememusicchecker D/ANDRO_ASYNC: opening connection
2019-04-04 20:19:22.515 495-528/? D/SurfaceFlinger: duplicate layer name: changing xmc.androidexpert35.com.xtrememusicchecker/xmc.androidexpert35.com.xtrememusicchecker.SettingsActivity to xmc.androidexpert35.com.xtrememusicchecker/xmc.androidexpert35.com.xtrememusicchecker.SettingsActivity#1
2019-04-04 20:19:22.585 495-794/? D/SurfaceFlinger: duplicate layer name: changing Surface(name=cafcbf6 xmc.androidexpert35.com.xtrememusicchecker/xmc.androidexpert35.com.xtrememusicchecker.SettingsActivity)/#0x3604cd - animation-leash to Surface(name=cafcbf6 xmc.androidexpert35.com.xtrememusicchecker/xmc.androidexpert35.com.xtrememusicchecker.SettingsActivity)/#0x3604cd - animation-leash#1
2019-04-04 20:19:22.614 15275-15275/xmc.androidexpert35.com.xtrememusicchecker I/ViewRootImpl: CPU Rendering VSync enable = true
2019-04-04 20:19:22.672 495-528/? W/SurfaceFlinger: Attempting to set client state on removed layer: xmc.androidexpert35.com.xtrememusicchecker/xmc.androidexpert35.com.xtrememusicchecker.SettingsActivity#1
2019-04-04 20:19:22.672 495-528/? W/SurfaceFlinger: Attempting to destroy on removed layer: xmc.androidexpert35.com.xtrememusicchecker/xmc.androidexpert35.com.xtrememusicchecker.SettingsActivity#1
2019-04-04 20:19:24.758 15275-15314/xmc.androidexpert35.com.xtrememusicchecker D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=3635, firebase_screen_class(_sc)=SettingsActivity, firebase_screen_id(_si)=-6495914915605520780}]
2019-04-04 20:19:26.125 829-3715/? W/NotificationService: Toast already killed. pkg=xmc.androidexpert35.com.xtrememusicchecker callback=android.app.ITransientNotification$Stub$Proxy#3b51651
This is my AsyncTask code, if anyone can help me find the issue? or suggest a useful debug solution to discover it?
Thanks, any help is very appreciated!
public class DownloadFile extends AsyncTask<String, String, String> {
private static String file_url;
private Context context;
private ProgressDialog dialog;
private String dialogString;
private File path;
private String xmpath;
private boolean canceled = false;
public DownloadFile(Context cxt) {
context = cxt;
dialog = new ProgressDialog(context);
}
#Override
protected void onPreExecute() {
dialog.setMessage(context.getString(R.string.xm_downloading));
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setCancelable(false);
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, context.getString(R.string.xm_cancel), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
path.delete();
canceled = true;
dialog.dismiss();
}
});
dialog.show();
super.onPreExecute();
}
#Override
protected String doInBackground(String... aurl) {
int count;
if (SettingsActivity.isMagisk){
file_url = "http://androidexpert35developer.altervista.org/Xtrememusic-versions/XTREMEMusic_MAGISK_OFICIAL_By_androidexpert35.zip";
path= new File(Environment.getExternalStorageDirectory() + "/XTREMEMusic_Download/XTREMEMusic_Magisk.zip");
}else{
file_url = "http://androidexpert35developer.altervista.org/Xtrememusic-versions/XTREMEMusic_OFFICIAL_By_androidexpert35.zip";
path = new File(Environment.getExternalStorageDirectory() + "/XTREMEMusic_Download/XTREMEMusic.zip");
Log.d("ANDRO_ASYNC","path set");
}
try {
Log.d("ANDRO_ASYNC","Try block");
URL url = new URL(file_url);
Log.d("ANDRO_ASYNC","file url got");
URLConnection conexion = url.openConnection();
Log.d("ANDRO_ASYNC","opening connection");
conexion.connect();
Log.d("ANDRO_ASYNC","Connected");
int lenghtOfFile = conexion.getContentLength();
InputStream is = url.openStream();
File testDirectory = new File(Environment.getExternalStorageDirectory() + "/XTREMEMusic_Download");
Log.d("ANDRO_ASYNC","making directory");
if (!testDirectory.exists()) {
testDirectory.mkdir();
}
FileOutputStream fos;
Log.d("ANDRO_ASYNC","Stream");
if(SettingsActivity.isMagisk) {
fos = new FileOutputStream(testDirectory + "/" + ("XTREMEMusic_Magisk") + ".zip");
Log.d("ANDRO_ASYNC","Downloading");
}else{
fos = new FileOutputStream(testDirectory + "/" + ("XTREMEMusic") + ".zip");
Log.d("ANDRO_ASYNC","Downloading");
}
byte data[] = new byte[1024];
long total = 0;
int progress = 0;
while ((count = is.read(data)) != -1) {
total += count;
int progress_temp = (int) total * 100 / lenghtOfFile;
publishProgress(""+ progress_temp);
if (progress_temp % 10 == 0 && progress != progress_temp) {
progress = progress_temp;
}
fos.write(data, 0, count);
}
is.close();
fos.close();
} catch (Exception e) {}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
dialog.setProgress(Integer.parseInt(progress[0]));
}
#Override
protected void onPostExecute(String unused) {
dialog.dismiss();
if(SettingsActivity.isInstall) {
installer();
}else if (canceled) {
Toast.makeText(context, R.string.xm_cancelled, Toast.LENGTH_LONG).show();
} else{
xmpath = path.toString();
Toast.makeText(context, context.getString(R.string.xm_downloaded, xmpath), Toast.LENGTH_LONG).show();
}
}
I getting issue while downloading a PDF file on click of button click in webview.
File is downloaded but the file is partly downloaded that's why i am getting below error
"The document cannot be opened because it is not a valid PDF document"
Below is Asyncetask activity of my to download file:
public class DownloadPDFTask extends AsyncTask<String, Void, Integer>
{
protected ProgressDialog mWorkingDialog; // progress dialog
protected String mFileName; // downloaded file
protected String mError; // for errors
#Override
protected Integer doInBackground(String... urls)
{
String filename = "";
String str[] = urls[2].split(";");
String st[] =str[1].split("=");
filename = st[1];
String extStorageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
File myDir = new File(extStorageDirectory, "NCR");
File file = new File(extStorageDirectory+"/NCR/"+filename);
// create the directory if it does not exist
if (!myDir.exists())
myDir.mkdirs();
if (file.exists()) {
System.out.println("INSIDE FILE EXIST");
file.delete();
}
try
{
byte[] dataBuffer = new byte[4096];
int nRead = 0;
mFileName = filename;
System.out.println("mFileName<><><> " +mFileName);
// download URL and store to strFileName
// connection to url
java.net.URL urlReport = new java.net.URL(urls[0]);
URLConnection urlConn = urlReport.openConnection();
urlConn.setRequestProperty("User-Agent", urls[1]);
urlConn.setRequestProperty("Content-Disposition", urls[2]);
urlConn.setRequestProperty("Content-Type", "application/pdf");
urlConn.setRequestProperty("Accept", "*/*");
InputStream streamInput = urlReport.openStream();
BufferedInputStream bufferedStreamInput = new BufferedInputStream(streamInput,8192);
FileOutputStream outputStream = new FileOutputStream(extStorageDirectory+"/NCR/"+mFileName);
while ((nRead = bufferedStreamInput.read(dataBuffer)) > 0)
outputStream.write(dataBuffer, 0, nRead);
streamInput.close();
outputStream.close();
// displayPdf(mFileName);
}
catch (Exception e)
{
Log.e("myApp", e.getMessage());
mError = e.getMessage();
return (1);
}
return (0);
}
#Override
protected void onPreExecute()
{
// show "Downloading, Please Wait" dialog
mWorkingDialog = ProgressDialog.show(context, "", "Downloading PDF Document, Please Wait...", true);
return;
}
#Override
protected void onPostExecute (Integer result)
{
if (mWorkingDialog != null)
{
mWorkingDialog.dismiss();
mWorkingDialog = null;
}
switch (result)
{
case 0: // a URL
try
{
displayPdf(mFileName);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(context, "No PDF Viewer Installed", Toast.LENGTH_LONG).show();
}
break;
case 1: // Error
Toast.makeText(context, mError, Toast.LENGTH_LONG).show();
break;
}
}
}
Friends I am stuck on this, Please help me out.
Hope this will help you. I tested this code and this is working fine.
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
int count;
try{
URL url=new URL(params[0]);
URLConnection connection=url.openConnection();
connection.connect();
//getting file length
long lengthOfFile=connection.getContentLength();
//input stream to read file with 8k buffer
InputStream input=new BufferedInputStream(url.openStream(),8192);
//out stream to write file
OutputStream output=new FileOutputStream(Environment.getExternalStorageDirectory()+"/Download/Test/software_testing.pdf");
byte data[]= new byte[1024];
long total =0;
while ((count = input.read(data)) != -1){
if(isCancelled())
return null;
total +=count;
//publishing the progress
//After this onProgressUpdate will be called
if(lengthOfFile > 0){
//System.out.println((int)((total*100)/lengthOfFile)+"First Line");
//Call onProgressUpdate() for display status
publishProgress((int)((total*100)/lengthOfFile));
}
//writing data to file
output.write(data,0,count);
}
//flushing output
output.flush();
//closing stream
output.close();
input.close();
}catch(Exception e){
Log.e("Error", e.getMessage());
System.out.println("Exception :"+e.getMessage());
}
return null;
}
EDITED:
Extend your class from AsyncTask<String, Integer, String> and override its' methods.
`
onPreExecute() used to do process before start the download.
doInBackground(String... params) used to do the process while
downloading the file. The above code is for this method.
onProgressUpdate(Integer... progress) used to do setting the
progress bar according to the current download percentage. Once you use publishProgress (), this method will invoke.
onPostExecute(String file_url) This method can used to dismiss the
dislog after the file was downloaded.
So what you have to do is set your progress bar to update according to the downloading percentage inside onProgressUpdate (Integer... progress). You can use setProgress() method for that.
I hope now you understand the process well :)
This might not be an issue, but your while loop isn't correct:
while ((nRead = bufferedStreamInput.read(dataBuffer)) > 0)
outputStream.write(dataBuffer, 0, nRead);
The BufferedInputStream.read() returns a -1 when it reaches the end of the stream.
Rather, your terminating condition should be:
while ((nRead = bufferedStreamInput.read(dataBuffer)) != -1)
outputStream.write(dataBuffer, 0, nRead);
I hope this helps.
I am trying to use my download code that works perfectly fine in my other app, but it is acting very strange in this new app I am trying to make.
public class DownloadFile extends AsyncTask<Void, Integer, String> {
String SDCardRoot;
String mixtapeurl = "http://xxxxxx.com/xxxxxxxxxxxx/Mixtapes/NCredible/J.%20Cole%20-%20Cole%20World.zip";
NotificationManager notificationManager;
Notification notification2;
ProgressBar progressBar;
#Override
protected void onPreExecute() {
// configure the intent
Intent intent = new Intent();
final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
// configure the notification
notification2 = new Notification(R.drawable.download, "DOWNLOADING: " + mixtapeurl, System
.currentTimeMillis());
notification2.flags = notification2.flags | Notification.FLAG_ONGOING_EVENT;
notification2.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.download_progress);
notification2.contentIntent = pendingIntent;
notification2.contentView.setTextViewText(R.id.percentage, 0 + "%" );
notification2.contentView.setTextViewText(R.id.status_text, "DOWNLOADING: " + mixtapeurl);
notification2.contentView.setProgressBar(R.id.status_progress, 100, 0, false);
getApplicationContext();
notificationManager = (NotificationManager) getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
notificationManager.notify(2, notification2);
}
#Override
protected String doInBackground(Void... params) {
try {
//set the download URL, a url that points to a file on the internet
//this is the file to be downloaded
URL url = new URL(mixtapeurl);
//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//set up some things on the connection
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
//and connect!
urlConnection.connect();
//set the path where we want to save the file
//in this case, going to save it on the root directory of the
//sd card.
SDCardRoot = Environment.getExternalStorageDirectory() + "/download/";
//create a new file, specifying the path, and the filename
//which we want to save the file as.
File file = new File(SDCardRoot,mixtapeurl);
//this will be used to write the downloaded data into the file we created
FileOutputStream fileOutput = new FileOutputStream(file);
//this will be used in reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file
int totalSize = urlConnection.getContentLength();
//variable to store total downloaded bytes
int downloadedSize = 0;
int myProgress;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
int previousProgress = 0;
//now, read through the input buffer and write the contents to the file
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
//add the data in the buffer to the file in the file output stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength);
//add up the size so we know how much is downloaded
downloadedSize += bufferLength;
//this is where you would do something to report the prgress, like this maybe
//updateProgress(downloadedSize, totalSize);
//publishProgress((int)(total*100/lenghtOfFile));
myProgress = (int) ((downloadedSize/(double)totalSize) * (double)100);
if ((myProgress) > 1 && (myProgress) > ((previousProgress ))) {
previousProgress= myProgress;
publishProgress(myProgress);
}
}
//close the output stream when done
fileOutput.close();
//catch some possible errors...
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "success";}
#Override
protected void onProgressUpdate(Integer... progression) {
String percent;
percent = progression[0].toString();
notification2.contentView.setProgressBar(R.id.status_progress, 100, progression[0], false);
notification2.contentView.setTextViewText(R.id.percentage, percent + "%" );
notificationManager.notify(2, notification2);
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
Toast toast = Toast.makeText(getApplicationContext(), mixtapeurl + " downloaded to: " + SDCardRoot, Toast.LENGTH_LONG);
toast.show();
}
}
Here is how I implement it:
new DownloadFile().execute();
And what i get is "http://xxxxxx.com/xxxxxxxxxxxx/Mixtapes/NCredible/J.%20Cole%20-%20Cole%20World.zip download to null" for my toast right after I click the button and the file does not download at all.
This sounds like a very common issue: Forgot to set permission in manifest.
<uses-permission android:name="android.permission.INTERNET" />
What I would like to do is give my app the ability to download my mp3 of my server. So far I have the download mp3 into a audio file working but it's very finicky and cannot be disturbed in order for it to work properly. That being said I would love to have a progress dialog pop up that cannot be canceled so the user can't interrupt the progress while downloading the file to the folder in the background. After reading it seemed that AsyncTask would be the best way to do this but I cannot get it to work. Below is one of the buttons from my code.
public class music extends Activity {
public static int mProgress = 0;
static String filename;
MediaPlayer buttonclicker;
static Toast msg;
public static int totalSize = 0;
public ProgressDialog dialog;
public static boolean isFinished;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music);
buttonclicker = MediaPlayer.create(this, R.raw.button );
Button boomFullDownload = (Button) findViewById(R.id.boomfull);
boomFullDownload.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonclicker.start();
filename = "boomboom.mp3";
new downloadPumphouseShow().execute(filename);
}
class downloadPumphouseShow extends AsyncTask<String , Void, Void> {
ProgressDialog dialog;
Toast msg;
protected void onPreExecute (){
dialog = new ProgressDialog(context);
msg = Toast.makeText(context, " File Exist ", Toast.LENGTH_LONG);
msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
dialog.setMessage("Please Wait Loading");
dialog.setCancelable(false);
dialog.show();
}
}
});
protected void onPostExecute(Void result) {
dialog.hide();
dialog.dismiss();
}
protected Void doInBackground(String... params) {
String filename = params[0];
try {
//set the download URL, a url that points to a file on the internet
//this is the file to be downloaded
URL url = new URL("http://lepumphouse.com/media/" + filename );
//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//set up some things on the connection
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
//and connect!
urlConnection.connect();
//set the path where we want to save the file
//in this case, going to save it on the root directory of the
//sd card.
File Music = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC + "/Pumphouse/Party Cake");
//create a new file, specifying the path, and the filename
if(Music.exists())
msg.show();
else
Music.mkdirs();
//which we want to save the file as.
File file = new File(Music, filename);
//this will be used to write the downloaded data into the file we created
FileOutputStream fileOutput = new FileOutputStream(file);
//this will be used in reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file
int totalSize = urlConnection.getContentLength();
//variable to store total downloaded bytes
int mProgress = 0;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
//now, read through the input buffer and write the contents to the file
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
//add the data in the buffer to the file in the file output stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength);
//add up the size so we know how much is downloaded
mProgress += bufferLength;
//this is where you would do something to report the pr0gress, like this maybe
}
//close the output stream when done
// progressDialog.dismiss();
fileOutput.close();
//catch some possible errors...
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
So if I stripped out all the code dealing with asynctask it works it's just extremely un-user friendly but the files do download. When I try to add the progress dialog and background task it quits on me. I have a feeling it has to do with the parameters.
protected void onPreExecute() {
dialog=ProgressDialog.show(mContext, "", "Fetching book oversight");
msg = Toast.makeText(context, " File Exist ", Toast.LENGTH_LONG).show;
super.onPreExecute();
}
protected void onPostExecute(Void result) {
if(dialog!=null)
{
dialog.dismiss();
}
}
Try this, a alternate way to show Dialog
Just a quick scan, I don't think you should be calling msg.show(); from the background thread.
In my activity, I create a Bitmap object and then I need to launch another Activity,
How can I pass this Bitmap object from the sub-activity (the one which is going to be launched)?
Bitmap implements Parcelable, so you could always pass it with the intent:
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);
and retrieve it on the other end:
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
Actually, passing a bitmap as a Parcelable will result in a "JAVA BINDER FAILURE" error. Try passing the bitmap as a byte array and building it for display in the next activity.
I shared my solution here:
how do you pass images (bitmaps) between android activities using bundles?
Passsing bitmap as parceable in bundle between activity is not a good idea because of size limitation of Parceable(1mb). You can store the bitmap in a file in internal storage and retrieve the stored bitmap in several activities. Here's some sample code.
To store bitmap in a file myImage in internal storage:
public String createImageFromBitmap(Bitmap bitmap) {
String fileName = "myImage";//no .png or .jpg needed
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE);
fo.write(bytes.toByteArray());
// remember close file output
fo.close();
} catch (Exception e) {
e.printStackTrace();
fileName = null;
}
return fileName;
}
Then in the next activity you can decode this file myImage to a bitmap using following code:
//here context can be anything like getActivity() for fragment, this or MainActivity.this
Bitmap bitmap = BitmapFactory.decodeStream(context.openFileInput("myImage"));
Note A lot of checking for null and scaling bitmap's is ommited.
If the image is too large and you can't save&load it to the storage, you should consider just using a global static reference to the bitmap (inside the receiving activity), which will be reset to null on onDestory, only if "isChangingConfigurations" returns true.
Compress and Send Bitmap
The accepted answer will crash when the Bitmap is too large. I believe it's a 1MB limit. The Bitmap must be compressed into a different file format such as a JPG represented by a ByteArray, then it can be safely passed via an Intent.
Implementation
The function is contained in a separate thread using Kotlin Coroutines because the Bitmap compression is chained after the Bitmap is created from an url String. The Bitmap creation requires a separate thread in order to avoid Application Not Responding (ANR) errors.
Concepts Used
Kotlin Coroutines notes.
The Loading, Content, Error (LCE) pattern is used below. If interested you can learn more about it in this talk and video.
LiveData is used to return the data. I've compiled my favorite LiveData resource in these notes.
In Step 3, toBitmap() is a Kotlin extension function requiring that library to be added to the app dependencies.
Code
1. Compress Bitmap to JPG ByteArray after it has been created.
Repository.kt
suspend fun bitmapToByteArray(url: String) = withContext(Dispatchers.IO) {
MutableLiveData<Lce<ContentResult.ContentBitmap>>().apply {
postValue(Lce.Loading())
postValue(Lce.Content(ContentResult.ContentBitmap(
ByteArrayOutputStream().apply {
try {
BitmapFactory.decodeStream(URL(url).openConnection().apply {
doInput = true
connect()
}.getInputStream())
} catch (e: IOException) {
postValue(Lce.Error(ContentResult.ContentBitmap(ByteArray(0), "bitmapToByteArray error or null - ${e.localizedMessage}")))
null
}?.compress(CompressFormat.JPEG, BITMAP_COMPRESSION_QUALITY, this)
}.toByteArray(), "")))
}
}
ViewModel.kt
//Calls bitmapToByteArray from the Repository
private fun bitmapToByteArray(url: String) = liveData {
emitSource(switchMap(repository.bitmapToByteArray(url)) { lce ->
when (lce) {
is Lce.Loading -> liveData {}
is Lce.Content -> liveData {
emit(Event(ContentResult.ContentBitmap(lce.packet.image, lce.packet.errorMessage)))
}
is Lce.Error -> liveData {
Crashlytics.log(Log.WARN, LOG_TAG,
"bitmapToByteArray error or null - ${lce.packet.errorMessage}")
}
}
})
}
2. Pass image as ByteArray via an Intent.
In this sample it's passed from a Fragment to a Service. It's the same concept if being shared between two Activities.
Fragment.kt
ContextCompat.startForegroundService(
context!!,
Intent(context, AudioService::class.java).apply {
action = CONTENT_SELECTED_ACTION
putExtra(CONTENT_SELECTED_BITMAP_KEY, contentPlayer.image)
})
3. Convert ByteArray back to Bitmap.
Utils.kt
fun ByteArray.byteArrayToBitmap(context: Context) =
run {
BitmapFactory.decodeByteArray(this, BITMAP_OFFSET, size).run {
if (this != null) this
// In case the Bitmap loaded was empty or there is an error I have a default Bitmap to return.
else AppCompatResources.getDrawable(context, ic_coinverse_48dp)?.toBitmap()
}
}
Because Intent has size limit .
I use public static object to do pass bitmap from service to broadcast ....
public class ImageBox {
public static Queue<Bitmap> mQ = new LinkedBlockingQueue<Bitmap>();
}
pass in my service
private void downloadFile(final String url){
mExecutorService.submit(new Runnable() {
#Override
public void run() {
Bitmap b = BitmapFromURL.getBitmapFromURL(url);
synchronized (this){
TaskCount--;
}
Intent i = new Intent(ACTION_ON_GET_IMAGE);
ImageBox.mQ.offer(b);
sendBroadcast(i);
if(TaskCount<=0)stopSelf();
}
});
}
My BroadcastReceiver
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
LOG.d(TAG, "BroadcastReceiver get broadcast");
String action = intent.getAction();
if (DownLoadImageService.ACTION_ON_GET_IMAGE.equals(action)) {
Bitmap b = ImageBox.mQ.poll();
if(b==null)return;
if(mListener!=null)mListener.OnGetImage(b);
}
}
};
It might be late but can help.
On the first fragment or activity do declare a class...for example
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
description des = new description();
if (requestCode == PICK_IMAGE_REQUEST && data != null && data.getData() != null) {
filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
constan.photoMap = bitmap;
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static class constan {
public static Bitmap photoMap = null;
public static String namePass = null;
}
Then on the second class/fragment do this..
Bitmap bm = postFragment.constan.photoMap;
final String itemName = postFragment.constan.namePass;
Hope it helps.
All of the above solutions doesn't work for me, Sending bitmap as parceableByteArray also generates error android.os.TransactionTooLargeException: data parcel size.
Solution
Saved the bitmap in internal storage as:
public String saveBitmap(Bitmap bitmap) {
String fileName = "ImageName";//no .png or .jpg needed
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE);
fo.write(bytes.toByteArray());
// remember close file output
fo.close();
} catch (Exception e) {
e.printStackTrace();
fileName = null;
}
return fileName;
}
and send in putExtra(String) as
Intent intent = new Intent(ActivitySketcher.this,ActivityEditor.class);
intent.putExtra("KEY", saveBitmap(bmp));
startActivity(intent);
and Receive it in other activity as:
if(getIntent() != null){
try {
src = BitmapFactory.decodeStream(openFileInput("myImage"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
You can create a bitmap transfer. try this....
In the first class:
1) Create:
private static Bitmap bitmap_transfer;
2) Create getter and setter
public static Bitmap getBitmap_transfer() {
return bitmap_transfer;
}
public static void setBitmap_transfer(Bitmap bitmap_transfer_param) {
bitmap_transfer = bitmap_transfer_param;
}
3) Set the image:
ImageView image = (ImageView) view.findViewById(R.id.image);
image.buildDrawingCache();
setBitmap_transfer(image.getDrawingCache());
Then, in the second class:
ImageView image2 = (ImageView) view.findViewById(R.id.img2);
imagem2.setImageDrawable(new BitmapDrawable(getResources(), classe1.getBitmap_transfer()));
In my case, the way mentioned above didn't worked for me. Every time I put the bitmap in the intent, the 2nd activity didn't start. The same happened when I passed the bitmap as byte[].
I followed this link and it worked like a charme and very fast:
package your.packagename
import android.graphics.Bitmap;
public class CommonResources {
public static Bitmap photoFinishBitmap = null;
}
in my 1st acitiviy:
Constants.photoFinishBitmap = photoFinishBitmap;
Intent intent = new Intent(mContext, ImageViewerActivity.class);
startActivity(intent);
and here is the onCreate() of my 2nd Activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bitmap photo = Constants.photoFinishBitmap;
if (photo != null) {
mViewHolder.imageViewerImage.setImageDrawable(new BitmapDrawable(getResources(), photo));
}
}