私のドロウアブルフォルダに画像があります。アクティビティでそれらが開き、必要な画像を選択してボタンをクリックします。それらはImageSavingTask
クラスインスタンス実行AsyncTask
を介して私のSDカードに保存する必要があります。その後、選択した画像がdoInBackground
方法で処理されAndroid - AsyncTaskでSDカードに画像を保存できない
@Override
public void onClick(View v) {
for (int i = 0; i < 26; i++)
if (checkBoxes[i].isChecked()) {
imageIndex = new ImageIndex(); //ImageIndex-a class with single index field which reserves the checked checkbox indexes.
imageIndex.index = i;
Bitmap bitmap = ((BitmapDrawable) (images[i].getDrawable())).getBitmap();
SaveImageTask saveImageTask = new SaveImageTask();
saveImageTask.execute(bitmap); //The class SaveImageTask extends AsyncTask<Bitmap, Void, Void>
}
}
:
は、ここに私のonClick
コードです。
@Override
protected Void doInBackground(Bitmap... params) {
FileOutputStream outStream = null;
try {
Bitmap bitmap = params[0];
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageBytes = stream.toByteArray();
File sdCard = Environment.getExternalStorageDirectory();
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
File dir = new File(sdCard.getAbsolutePath());
dir.mkdirs();
String fileName = "Saved image " + imageIndex.index; //The reserved index of checkbox creates a name for the new file.
File outFile = new File(dir, fileName);
outStream = new FileOutputStream(outFile);
outStream.write(imageBytes);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
行が私のマニフェストに追加されました。
USBを電話機に接続しても、エラーは発生しませんが、SDカードに画像が保存されません。そして私は窓の検索を使用して私の携帯電話で画像を見つけることができません。デバッグは何の答えも出さない。これはどんな種類の問題でしょうか?
SaveImageTask saveImageTask = new SaveImageTask(); 'タスクに直接インデックスを付ける必要があります。すべてのタスクが同じになるので、今のようにグローバルではありません。より良い: 'SaveImageTask saveImageTask =新しいSaveImageTask(i);'。 – greenapps
'Environment.getExternalStorageDirectory();'。 SDカードに保存するのではなく、外部ストレージに保存します。 – greenapps
デバイスにファイルエクスプローラを使用して、画像が存在するかどうかを確認する必要があります。実際には、新しいファイルについてMediaStoreに通知していないので、PCでそれらを見ることはできません。実際にファイルが作成された場合は、デバイスの電源を入れ直した後にPC上に表示されます。 – greenapps