How to save bitmap to image file in Download folder from your Android App? Android 14 | API level 34
Автор: Programmer World
Загружено: 2023-10-04
Просмотров: 1465
Описание:
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/ho...
https://programmerworld.co/android/ho...
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: [email protected]
Complete source code and other details/ steps of this video are posted in the below link:
https://programmerworld.co/android/ho...
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/...");
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);
}
}
}
--
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: