1

こんにちは、画像をキャプチャして電子メールで送信するアプリを開発しています。画像をキャプチャすると、Samsung Galaxy と Sony Ericsson xperia で正常に動作しますが、HTC デバイスでは動作しません。ここに画像をキャプチャするための私のコードがあります

try {
    String fileName = Image_name+".jpg";
    //create parameters for Intent with filename
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, fileName);
    values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");
    //imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
    outuri = getContentResolver().insert(
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    //outuri = Uri.fromFile(photo);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outuri);
    cameraIntent.putExtra("return-data", true);
    startActivityForResult(cameraIntent, 2);
} catch (Exception e) {
    Toast.makeText(preview.this, ""+e, Toast.LENGTH_LONG).show();
}

画像を取得するために使用するコードは次のとおりです

path = convertImageUriToFile(outuri, preview.this).getAbsolutePath();
arr.add(path);
try {
    bitmap = getImage(path);
    public static File convertImageUriToFile (Uri imageUri, Activity activity)  {
        Cursor cursor = null;
        try {
            String [] proj={MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.ORIENTATION};
            cursor = activity.managedQuery(imageUri, proj, // Which columns to return
                    null,       // WHERE clause; which rows to return (all rows)
                    null,       // WHERE clause selection arguments (none)
                    null); // Order-by clause (ascending by name)
            int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            int orientation_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION);
            if (cursor.moveToFirst()) {
                String orientation =  cursor.getString(orientation_ColumnIndex);
                return new File(cursor.getString(file_ColumnIndex));
            }
            return null;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    public Bitmap getImage(String path) throws IOException
    {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        Bitmap targetBitmap=null;
        int srcWidth = options.outWidth;
        int srcHeight = options.outHeight;
        int[] newWH =  new int[2];
        newWH[0] = 1024;
        newWH[1] = (1024*srcHeight)/srcWidth;

        int inSampleSize = 1;
        while(srcWidth / 2 > newWH[0]){
            srcWidth /= 2;
            srcHeight /= 2;
            inSampleSize *= 2;
        }
//      float desiredScale = (float) newWH[0] / srcWidth;
        // Decode with inSampleSize
        options.inJustDecodeBounds = false;
        options.inDither = false;
        options.inSampleSize = inSampleSize;
        options.inScaled = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(path,options);
        ExifInterface exif = new ExifInterface(path);
        String s=exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        System.out.println("Orientation>>>>>>>>>>>>>>>>>>>>"+s);
        Matrix matrix = new Matrix();
        float rotation = rotationForImage(preview.this, Uri.fromFile(new File(path)));
        if (rotation != 0f) {
            matrix.preRotate(rotation);
        }
        int newh = ( w * sampledSrcBitmap.getHeight() ) /sampledSrcBitmap.getWidth();
        Bitmap r=Bitmap.createScaledBitmap(sampledSrcBitmap, w, newh, true);
        Bitmap resizedBitmap = Bitmap.createBitmap(
                r, 0, 0, w, newh, matrix, true);

        return resizedBitmap;
    }
}
4

1 に答える 1

2

に既知のバグがありIntent.putExtra(MediaStore.EXTRA_OUTPUT)、アプリでクラッシュが発生します。

同じ質問をしたときに私が得た答えをチェックしてください:https ://stackoverflow.com/a/10613299/1056359

于 2012-07-02T06:03:37.003 に答える