写真を撮るためにボタンを押すとクラッシュするアンドロイドアプリケーションを開発しています。写真をフォルダに保存してアプリケーションにサムネイルを表示する必要がありますが、写真を撮るとアプリがクラッシュし、それにもかかわらず、目的のフォルダに写真を保存します。アプリの写真保存部分を実装する前に、サムネイルを表示していました。インテントデータにアクセスしようとするとアプリがクラッシュするのはなぜですか?
アプリケーションがクラッシュがコード(onActivityResultオーバーライド機能)のこのブロックに到達後:
if(data.getData() == null) {
thumbnail.add((Bitmap)data.getExtras().get("data"));
}
if(thumbnail.get(0) != null && thumbnail.size() > 0)
{
for(int i=0; i < thumbnail.size();i++){
//Toast.makeText(getApplicationContext(), Integer.toString(thumbnail.size()), Toast.LENGTH_SHORT).show();
createPhotoThumbnail(thumbnail);
}
}
これはカメラディスパッチ意図関数である:
private void dispatchCameraIntent(){
Intent takePicIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePicIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the Files
Toast.makeText(this, "Could not create file", Toast.LENGTH_SHORT).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = Uri.fromFile(photoFile);
takePicIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
takePicIntent.putExtra(android.provider.MediaStore.EXTRA_SIZE_LIMIT, "720000");
startActivityForResult(takePicIntent, REQUEST_IMAGE_CAPTURE);
setResult(RESULT_OK, takePicIntent);
}
} else
{
//Make a toast if there is no camera app installed.
Toast toast = Toast.makeText(this,"No program to take pictures",Toast.LENGTH_SHORT);
toast.show();
}
}
I」はveはlogcatを追加しました:
FATAL EXCEPTION: main
Process: lt.vilniausbaldai.vilniausbaldaiofflinedefektai, PID: 14330
Theme: themes:{}
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {lt.vilniausbaldai.vilniausbaldaiofflinedefektai/lt.vilniausbaldai.vilniausbaldaiofflinedefektai.MainActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at android.app.ActivityThread.deliverResults(ActivityThread.java:3733)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at lt.vilniausbaldai.vilniausbaldaiofflinedefektai.MainActivity.onActivityResult(MainActivity.java:135)
at android.app.Activity.dispatchActivityResult(Activity.java:6456)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3729)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
createPhotoThumbnail方法:
private void createPhotoThumbnail(ArrayList<Bitmap> thumbnail){
this.imageGrid = (GridView) findViewById(R.id.gridView);
this.bitmapList = new ArrayList<>();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
try {
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.close();
}
catch (IOException e) {
e.printStackTrace();
}
try {
for(int i = 0; i < thumbnail.size(); i++) {
thumbnail.get(i).compress(Bitmap.CompressFormat.JPEG, 100, bytes);
this.bitmapList.add(thumbnail.get(i));
}
} catch (Exception e) {
e.printStackTrace();
}
this.imageGrid.setAdapter(new ImageAdapter(this, this.bitmapList));
imageGrid.invalidate();
}
createImageFile方法:
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date());
//String timeStamp = "picTesting";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
timeStamp, /* prefix */
".png", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
//Toast.makeText(this, mCurrentPhotoPath, Toast.LENGTH_LONG).show();
return image;
}
を –
はあなたが権限を追加したあなたのスタックトレースを表示してください? – Alikbar
はい、android.permission.CAMERAとWRITE_EXTERNAL_STORAGEを追加しました。また、も追加しました。 –