2

アプリの写真を撮るためにカメラまたはギャラリーのいずれかを使用していますが、カメラから画像をクリックしてからギャラリーに切り替えた後、ギャラリーがクラッシュすることがあります。

コードを以下に示します。

case R.id.etUploadImage:
        Log.d(TAG, " add photo");
        if (!Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            Toast.makeText(getApplicationContext(), "sdcard not mounted",
                    Toast.LENGTH_SHORT).show();
            break;
        }
        AlertDialog.Builder photoDialog = new AlertDialog.Builder(this);
        photoDialog
                .setTitle("Photo source")
                .setCancelable(true)

                .setPositiveButton("Open Gallery",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                startActivityForResult(
                                        new Intent(
                                                Intent.ACTION_PICK,
                                                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI),
                                        54);
                            }
                        })

                .setNegativeButton("Open Camera",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int id) {
                                //String fileName = "temp.jpg";
                                Date date = new Date();
                                DateFormat df = new SimpleDateFormat("-mm-ss");

                                String newPicFile = "Bild"+ df.format(date) + ".jpg";
                                String outPath = "/sdcard/" + newPicFile;
                                File outFile = new File(outPath);

                                ContentValues values = new ContentValues();
                                values.put(MediaStore.Images.Media.TITLE,
                                        outFile.getAbsolutePath());
                                mCapturedImageURI = getContentResolver()
                                        .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                                values);

                                Intent intent = new Intent(
                                        MediaStore.ACTION_IMAGE_CAPTURE);
                                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                                        mCapturedImageURI);

                                startActivityForResult(intent, 96);

                            }
                        });
        photoDialog.show();
        break;
    }

onActivityResult()は次のとおりです。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 54 && resultCode == RESULT_OK) {
        if(data !=null){
        fileName = getRealPathFromURI(data.getData());
        mUploadImage.setText(getStringNameFromRealPath((String) fileName));
        } else {
            Toast.makeText(getApplicationContext(),"Please select another image.", Toast.LENGTH_SHORT).show();
        }
    } else if (requestCode == 96 && resultCode != 0) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(mCapturedImageURI, projection, null,
                null, null);
        int column_index_data = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        fileName = cursor.getString(column_index_data);
        String s[] = fileName.split("/");
        mUploadImage.setText(s[s.length - 1]);
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mCapturedImageURI));
    }
}

ギャラリーをクラッシュさせているのは何ですか。助けてください。

4

1 に答える 1

3

だから、多くのジャグリングの後、私のために働いたこの解決策を見つけました..

ギャラリーまたはカメラを開くには:-

if (!Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            Toast.makeText(getApplicationContext(), "sdcard not mounted",
                    Toast.LENGTH_SHORT).show();
            break;
        }
        AlertDialog.Builder photoDialog = new AlertDialog.Builder(this);
        photoDialog
                .setTitle("Photo source")
                .setCancelable(true)

                .setPositiveButton("Open Gallery",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                startActivityForResult(
                                        new Intent(
                                                Intent.ACTION_PICK,
                                                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI),
                                        54);
                            }
                        })

                .setNegativeButton("Open Camera",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int id) {


                                int apiLevel = Integer.parseInt(Build.VERSION.SDK);
                                if(apiLevel < 11) {
                                try{
                                //String fileName = "temp.jpg";

                                Calendar c = Calendar.getInstance();
                                CAPTURE_TITLE = "si_"+c.getTimeInMillis()+".jpg";
                                File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera", CAPTURE_TITLE);
                                Uri ImageURI = Uri.fromFile(file);


                                Intent intent = new Intent(
                                        MediaStore.ACTION_IMAGE_CAPTURE);
                                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                                        ImageURI    );

                                startActivityForResult(intent, 99);

                                }
                                catch(Exception e)
                                {
                                    LogCreator lg = new LogCreator();
                                    lg.writeBarcodeToFile("Image exc1", e.toString());
                                }

                                }
                                else{

                                    //String fileName = "temp.jpg";
                                    Date date = new Date();
                                    DateFormat df = new SimpleDateFormat("-mm-ss");

                                    String newPicFile = "Bild"+ df.format(date) + ".jpg";
                                    String outPath = "/sdcard/" + newPicFile;
                                    File outFile = new File(outPath);

                                    ContentValues values = new ContentValues();
                                    values.put(MediaStore.Images.Media.TITLE,
                                            outFile.getAbsolutePath());
                                    mCapturedImageURI = getContentResolver()
                                            .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                                    values);

                                    Intent intent = new Intent(
                                            MediaStore.ACTION_IMAGE_CAPTURE);
                                    intent.putExtra(MediaStore.EXTRA_OUTPUT,
                                            mCapturedImageURI);

                                    startActivityForResult(intent, 96);
                                }
                            }


                        });
        photoDialog.show();

そして今 onActivityResult()

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 54 && resultCode == RESULT_OK) {
        if(data !=null){
        fileName = getRealPathFromURI(data.getData());
        mUploadImage.setText(getStringNameFromRealPath((String) fileName));
        } else {
            Toast.makeText(getApplicationContext(),"Please select another image.", Toast.LENGTH_SHORT).show();
        }
    } 
    else if (requestCode == 99 && resultCode != 0) {
        try {
            File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera", CAPTURE_TITLE);
            fileName = Environment.getExternalStorageDirectory() + "/DCIM/Camera/"+CAPTURE_TITLE;

            Uri imgUri = Uri.fromFile(file);
            //new GetThumbnail().execute(imgUri);
            mUploadImage.setText(CAPTURE_TITLE);
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse("file://"
                            + Environment.getExternalStorageDirectory())));

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
        else if (requestCode == 96 && resultCode != 0) {
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(mCapturedImageURI, projection, null,
                    null, null);
            int column_index_data = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            fileName = cursor.getString(column_index_data);
            String s[] = fileName.split("/");
            mUploadImage.setText(s[s.length - 1]);
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mCapturedImageURI));
        }






}

これらは、パスを取得するために使用したメソッドです:-

/*
 * Returns image real path.
 */
private String getRealPathFromURI(final Uri contentUri) {
    final String[] proj = { MediaStore.Images.Media.DATA };
    @SuppressWarnings("deprecation")
    final Cursor cursor = managedQuery(contentUri, proj, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();

    return cursor.getString(column_index);
}

/*
 * Cuts selected file name from real path to show in screen.
 */
private String getStringNameFromRealPath(final String bucketName) {
    return bucketName.lastIndexOf('/') > 0 ? bucketName
            .substring(bucketName.lastIndexOf('/') + 1) : bucketName;
}

このヘルプを願っています。

于 2013-05-02T12:25:40.487 に答える