2016-10-11 6 views
0

私は彼のカメラから写真を撮って、それをメモリに内部的に保存して動作させようとしています。私はメソッドを作成した画像のサイズを変更し、それを達成する方法を見つけることを試みるが、私は私が掲載いくつかのソリューションを追加しようとしたことonActivityResultデータがカメラでヌル

public void takePicture() { 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new  Date()); 
    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "App"); 
    imagesFolder.mkdirs(); 

    Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    File image = new File(imagesFolder, "QR_" + timeStamp + ".jpg"); 
    Uri uriSavedImage = Uri.fromFile(image); 
    uriString=uriSavedImage.toString(); 
    imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); 
    startActivityForResult(imageIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 

} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode==CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE){ 
     Uri uri =data.getData(); 
     changeImageSize(uri,500,500); 


    } 
} 

private Bitmap changeImageSize(Uri fileUri, int width, int height) { 
    Bitmap bitmap = null; 
    File imgFile = new File(getRealPathFromURI(getApplicationContext(),fileUri)); 
    if (imgFile.exists()) { 
     bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 
    } 
    if (bitmap != null) { 
     return bitmap = (Bitmap.createScaledBitmap(bitmap, width, height, false)); 
    } 
    return null; 
} 



    public String getRealPathFromURI(Context context, Uri contentUri) { 
    Cursor cursor = getActivity().getContentResolver().query(contentUri, null, null, null, null); 
    cursor.moveToFirst(); 
    String document_id = cursor.getString(0); 
    document_id = document_id.substring(document_id.lastIndexOf(":")+1); 
    cursor.close(); 

    cursor = getActivity().getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,null 
      , MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null); 
    cursor.moveToFirst(); 
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); 
    cursor.close(); 

    return path; 
} 

アクセスすることはできませんので、問題がonActivityのみのデータにnullを返しているときに問題があります怒鳴ると、今、私はこれを述べる私のgetRealPathFromURIメソッドからエラーを取得しています:

Logcat

Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference 
java.lang.RuntimeException: Failure delivering result 
ResultInfo{who=null, request=65636, result=-1, data=null} to activity {com.app/com.app.MainPostLogin}: java.lang.NullPointerException: 
Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference 
+0

changeImageSizeメソッドに問題があります。親切にコード –

+0

を投稿すると、このように作成されたアクティビティの保存uriパスが使用されます。 'Uri uri = uriSavedImage; changeImageSize(uri、500,500); ' – prakash

+0

デバイスの中にはnull値を返すものがありますので、このように試してみてください。 – prakash

答えて

-1

これは私がイメージのサイズを変更するために私のプロジェクトで使用した作業コードです。 このメソッドを作成します(ただ、ペーストをコピー):

private String decodeFile(String path, int DESIREDWIDTH, int DESIREDHEIGHT) { 
    String strMyImagePath = null; 
    Bitmap scaledBitmap = null; 

    try { 
     // Part 1: Decode image 
     Bitmap unscaledBitmap = ScalingUtilities.decodeFile(path, DESIREDWIDTH, DESIREDHEIGHT, ScalingUtilities.ScalingLogic.FIT); 

     if (!(unscaledBitmap.getWidth() <= DESIREDWIDTH && unscaledBitmap.getHeight() <= DESIREDHEIGHT)) { 
      // Part 2: Scale image 
      scaledBitmap = ScalingUtilities.createScaledBitmap(unscaledBitmap, DESIREDWIDTH, DESIREDHEIGHT, ScalingUtilities.ScalingLogic.FIT); 
     } else { 
      unscaledBitmap.recycle(); 
      return path; 
     } 

     // Store to tmp file 

     String extr = Environment.getExternalStorageDirectory().toString(); 
     File mFolder = new File(extr + "/TMMFOLDER"); 
     if (!mFolder.exists()) { 
      mFolder.mkdir(); 
     } 

     String s = "tmp.png"; 

     File f = new File(mFolder.getAbsolutePath(), s); 

     strMyImagePath = f.getAbsolutePath(); 
     FileOutputStream fos = null; 
     try { 
      fos = new FileOutputStream(f); 
      scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 75, fos); 
      fos.flush(); 
      fos.close(); 
     } catch (FileNotFoundException e) { 

      e.printStackTrace(); 
     } catch (Exception e) { 

      e.printStackTrace(); 
     } 

     scaledBitmap.recycle(); 
    } catch (Throwable e) { 
    } 

    if (strMyImagePath == null) { 
     return path; 
    } 
    return strMyImagePath; 
} 

は(ジャストペーストをコピーする)このクラスを作成します。

package com.katariya.pomoc.utils; 

/** 
* Created by Danish.sharma on 5/31/2016. 
*/ 
import android.content.res.Resources; 
import android.graphics.Bitmap; 
import android.graphics.Bitmap.Config; 
import android.graphics.BitmapFactory; 
import android.graphics.BitmapFactory.Options; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Rect; 

/** 
* Class containing static utility methods for bitmap decoding and scaling 
* 
* @author 
*/ 
public class ScalingUtilities { 

    /** 
    * Utility function for decoding an image resource. The decoded bitmap will 
    * be optimized for further scaling to the requested destination dimensions 
    * and scaling logic. 
    * 
    * @param res The resources object containing the image data 
    * @param resId The resource id of the image data 
    * @param dstWidth Width of destination area 
    * @param dstHeight Height of destination area 
    * @param scalingLogic Logic to use to avoid image stretching 
    * @return Decoded bitmap 
    */ 
    public static Bitmap decodeResource(Resources res, int resId, int dstWidth, int dstHeight, 
             ScalingLogic scalingLogic) { 
     Options options = new Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeResource(res, resId, options); 
     options.inJustDecodeBounds = false; 
     options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, dstWidth, 
       dstHeight, scalingLogic); 
     Bitmap unscaledBitmap = BitmapFactory.decodeResource(res, resId, options); 

     return unscaledBitmap; 
    } 
    public static Bitmap decodeFile(String path, int dstWidth, int dstHeight, 
            ScalingLogic scalingLogic) { 
     Options options = new Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, options); 
     options.inJustDecodeBounds = false; 
     options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, dstWidth, 
       dstHeight, scalingLogic); 
     Bitmap unscaledBitmap = BitmapFactory.decodeFile(path, options); 

     return unscaledBitmap; 
    } 

    /** 
    * Utility function for creating a scaled version of an existing bitmap 
    * 
    * @param unscaledBitmap Bitmap to scale 
    * @param dstWidth Wanted width of destination bitmap 
    * @param dstHeight Wanted height of destination bitmap 
    * @param scalingLogic Logic to use to avoid image stretching 
    * @return New scaled bitmap object 
    */ 
    public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int dstWidth, int dstHeight, 
              ScalingLogic scalingLogic) { 
     Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), 
       dstWidth, dstHeight, scalingLogic); 
     Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), 
       dstWidth, dstHeight, scalingLogic); 
     Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(), 
       Config.ARGB_8888); 
     Canvas canvas = new Canvas(scaledBitmap); 
     canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG)); 

     return scaledBitmap; 
    } 

    /** 
    * ScalingLogic defines how scaling should be carried out if source and 
    * destination image has different aspect ratio. 
    * 
    * CROP: Scales the image the minimum amount while making sure that at least 
    * one of the two dimensions fit inside the requested destination area. 
    * Parts of the source image will be cropped to realize this. 
    * 
    * FIT: Scales the image the minimum amount while making sure both 
    * dimensions fit inside the requested destination area. The resulting 
    * destination dimensions might be adjusted to a smaller size than 
    * requested. 
    */ 
    public static enum ScalingLogic { 
     CROP, FIT 
    } 

    /** 
    * Calculate optimal down-sampling factor given the dimensions of a source 
    * image, the dimensions of a destination area and a scaling logic. 
    * 
    * @param srcWidth Width of source image 
    * @param srcHeight Height of source image 
    * @param dstWidth Width of destination area 
    * @param dstHeight Height of destination area 
    * @param scalingLogic Logic to use to avoid image stretching 
    * @return Optimal down scaling sample size for decoding 
    */ 
    public static int calculateSampleSize(int srcWidth, int srcHeight, int dstWidth, int dstHeight, 
              ScalingLogic scalingLogic) { 
     if (scalingLogic == ScalingLogic.FIT) { 
      final float srcAspect = (float)srcWidth/(float)srcHeight; 
      final float dstAspect = (float)dstWidth/(float)dstHeight; 

      if (srcAspect > dstAspect) { 
       return srcWidth/dstWidth; 
      } else { 
       return srcHeight/dstHeight; 
      } 
     } else { 
      final float srcAspect = (float)srcWidth/(float)srcHeight; 
      final float dstAspect = (float)dstWidth/(float)dstHeight; 

      if (srcAspect > dstAspect) { 
       return srcHeight/dstHeight; 
      } else { 
       return srcWidth/dstWidth; 
      } 
     } 
    } 

    /** 
    * Calculates source rectangle for scaling bitmap 
    * 
    * @param srcWidth Width of source image 
    * @param srcHeight Height of source image 
    * @param dstWidth Width of destination area 
    * @param dstHeight Height of destination area 
    * @param scalingLogic Logic to use to avoid image stretching 
    * @return Optimal source rectangle 
    */ 
    public static Rect calculateSrcRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight, 
             ScalingLogic scalingLogic) { 
     if (scalingLogic == ScalingLogic.CROP) { 
      final float srcAspect = (float)srcWidth/(float)srcHeight; 
      final float dstAspect = (float)dstWidth/(float)dstHeight; 

      if (srcAspect > dstAspect) { 
       final int srcRectWidth = (int)(srcHeight * dstAspect); 
       final int srcRectLeft = (srcWidth - srcRectWidth)/2; 
       return new Rect(srcRectLeft, 0, srcRectLeft + srcRectWidth, srcHeight); 
      } else { 
       final int srcRectHeight = (int)(srcWidth/dstAspect); 
       final int scrRectTop = (int)(srcHeight - srcRectHeight)/2; 
       return new Rect(0, scrRectTop, srcWidth, scrRectTop + srcRectHeight); 
      } 
     } else { 
      return new Rect(0, 0, srcWidth, srcHeight); 
     } 
    } 

    /** 
    * Calculates destination rectangle for scaling bitmap 
    * 
    * @param srcWidth Width of source image 
    * @param srcHeight Height of source image 
    * @param dstWidth Width of destination area 
    * @param dstHeight Height of destination area 
    * @param scalingLogic Logic to use to avoid image stretching 
    * @return Optimal destination rectangle 
    */ 
    public static Rect calculateDstRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight, 
             ScalingLogic scalingLogic) { 
     if (scalingLogic == ScalingLogic.FIT) { 
      final float srcAspect = (float)srcWidth/(float)srcHeight; 
      final float dstAspect = (float)dstWidth/(float)dstHeight; 

      if (srcAspect > dstAspect) { 
       return new Rect(0, 0, dstWidth, (int)(dstWidth/srcAspect)); 
      } else { 
       return new Rect(0, 0, (int)(dstHeight * srcAspect), dstHeight); 
      } 
     } else { 
      return new Rect(0, 0, dstWidth, dstHeight); 
     } 
    } 

} 

これは、画像

Bitmap bmp = BitmapFactory.decodeFile(decodeFile(file.toString(), 600, 600)); 
           imageView.setImageBitmap(bmp); 

参照のサイズを変更するには、メインラインであります私のonActivityResultはどのようにしてこれらの行を使用していますか:

@Override 
    protected void onActivityResult(int requestCode, int resultCode, final Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 


     if (requestCode == PICK_IMAGE_REQUEST) { 
      new Thread(new Runnable() { 
       @Override 
       public void run() { 
        runOnUiThread(new Runnable() { 
         public void run() { 
          try { 
           Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
           ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
           thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes); 

           File destination = new File(Environment.getExternalStorageDirectory() + "/Guesp"); 
           if (!destination.exists()) { 
            File f = new File("/sdcard/POMOC/"); 
            f.mkdir(); 
           } 
           File file = new File(new File("/sdcard/POMOC/"), System.currentTimeMillis() + ".jpg"); 

           FileOutputStream fo; 
           try { 
            file.createNewFile(); 
            fo = new FileOutputStream(file); 
            fo.write(bytes.toByteArray()); 
            fo.close(); 
            picturePath = file.toString(); 
           } catch (FileNotFoundException e) { 
            e.printStackTrace(); 
           } catch (IOException e) { 
            e.printStackTrace(); 
           } 

           Bitmap bmp = BitmapFactory.decodeFile(decodeFile(file.toString(), 600, 600)); 
           img_profilePhoto.setImageBitmap(bmp); 
          } catch (Exception e) { 

           e.printStackTrace(); 
          } 
         } 
        }); 
       } 
      }).start(); 
     } 
    } 

それはそれです。

0

Uriをカメラに渡すと、データは返されません。このファイルをローカル変数File image = new File(imagesFolder, "QR_" + timeStamp + ".jpg");に保存して試してみてください。

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
super.onActivityResult(requestCode, resultCode, data); 
if (requestCode==CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK){ 
    changeImageSize(Uri.fromFile(image),500,500); 
    } 
} 
関連する問題