Ich versuche, mein Bild von imageView in Bitmap zu speichern, so dass ich es in der Galerie des Android-Geräts speichern kann. Jedes Mal, wenn ich ein Bild speichere, wird der Hintergrund von imageView nicht gespeichert. Was vermisse ich?Wie Hintergrundbild von ImageView in Bitmap speichern?
Hier ist mein Code:
ImageView imageView = (ImageView) findViewById(R.id.img);
imageView.setBackgroundResource(R.drawable.img1);
BitmapDrawable draw = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = draw.getBitmap();
Code, um das Bild in der Galerie zu speichern ist:
FileOutputStream outStream = null;
File dir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyAlbum");
if (!dir.exists()) {
if (!dir.mkdirs()) {
Log.d("MyAlbum", "failed to create directory");
Toast.makeText(MainActivity.this, "Failed to make directory", Toast.LENGTH_SHORT).show();
}
}
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
try {
outStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
Toast.makeText(getApplicationContext(), "PICTURE SAVED", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(dir));
sendBroadcast(intent);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Haben Sie einen Adapter? Können Sie verwandte Standorte Code – propoLis
wo ist der Rest-Code, um das Bild in den Speicher zu speichern ?? –
@ankitDemonstrate Sie können den Code sehen, um das Bild in den Speicher jetzt zu speichern –