imageViewビットマップ画像の保存ボタンがあります。クリックすると、同じ画像が 3 つ表示されますが、そのうちの 2 つが 0 バイトです。画像は次のとおりです。
そして、ここに私のコードがあります:
public class Photo_gallery extends Activity implements OnClickListener{
Button save;
ImageView slika;
final File myDir = new File("/sdcard/Pictures/sexyImages");
boolean success = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo_gallery);
InitializeVars();
}
private void InitializeVars() {
slika = (ImageView) findViewById(R.id.imageView1);
save = (Button) findViewById(R.id.bSave);
save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
final String fname = "StyleMe-" + n + ".png";
myDir.mkdirs();
File image = new File(myDir, fname);
BitmapDrawable drawable = (BitmapDrawable) slika.getDrawable();
Bitmap bitmap = drawable.getBitmap();
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), "Image saved with success at /sdcard/Pictures/sexyImages",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_LONG).show();
}
}
});
}
そして 2 番目の質問....ギャラリー アプリを更新するにはどうすればよいですか? このコードをどこかに実装する必要があることはわかっていますが、方法がわかりません。
Environment.getExternalStorageDirectory();