Загрузка...

How to save bitmap to image file in Download folder from your Android App? Android 14 | API level 34

In this video it shows the steps to save a bitmap to image file in download folder from your Android App programmatically. It demonstrates this for Android 14 - API level 34

In this video it refers to the code from my below pages of posted earlier:
https://programmerworld.co/android/how-to-fix-the-error-only-mutable-bitmaps-may-be-reconfigured-convert-immutable-bitmap-to-a-mutable-bitmap-in-android-app/
https://programmerworld.co/android/how-to-create-and-save-an-image-file-in-download-folder-from-your-android-app-android-13-api-33/
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com

Complete source code and other details/ steps of this video are posted in the below link:
https://programmerworld.co/android/how-to-save-bitmap-to-an-image-file-in-download-folder-from-your-android-app-android-14-api-level-34/
However, the main Java code is copied below also for reference:

package com.programmerworld.savebitmaptoimagefile;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

private ImageView imageView;
private URL url;
private Bitmap bitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imageView = findViewById(R.id.imageView);

Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
url = new URL("https://i0.wp.com/programmerworld.co/wp-content/uploads/2023/05/flower.png?w=977&ssl=1");
bitmap = BitmapFactory.decodeStream(url.openStream());

} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
thread.start();
}

public void buttonSaveImage(View view){
imageView.setImageBitmap(bitmap);
StorageManager storageManager = (StorageManager) getSystemService(STORAGE_SERVICE);
StorageVolume storageVolume = storageManager.getStorageVolumes().get(0); // internal Storage
File fileImage = new File(storageVolume.getDirectory().getPath() + "/Download/"
+ System.currentTimeMillis()
+ ".jpeg");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] bytesArray = byteArrayOutputStream.toByteArray();
try {
FileOutputStream fileOutputStream = new FileOutputStream(fileImage);
fileOutputStream.write(bytesArray);
fileOutputStream.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

--

Видео How to save bitmap to image file in Download folder from your Android App? Android 14 | API level 34 канала Programmer World
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять