2016-09-26 98 views
2

我使用相机意图让用户捕捉照片并将其发送到其他活动以供进一步使用。我包括EXTRA_OUTPUT部分在putExtra相机意图的方法,以便我可以得到完整大小的图像,而不是缩略图。问题是,捕获图像后,我被重定向到MainActivity。当我在中看到文件管理器时,我发现图像已保存。请帮忙。Android中的相机意图问题

CameraButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
       String imageFileName = timeStamp + ".jpg"; 
       File storageDir = Environment.getExternalStoragePublicDirectory(
         Environment.DIRECTORY_PICTURES); 
       pictureImagePath = storageDir.getAbsolutePath() + "/" + imageFileName; 
       file = new File(pictureImagePath); 
       outputFileUri = Uri.fromFile(file); 
       Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       camera.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
       startActivityForResult(camera, REQUEST_IMAGE_CAPTURE); 
      } 
     }); 


@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK && null != data) { 
try { 

        b = MediaStore.Images.Media.getBitmap(this.getContentResolver(), outputFileUri); 
        String filename123 = "myphoto.jpg"; 
        FileOutputStream out = this.openFileOutput(filename123, Context.MODE_PRIVATE); 
        b.compress(Bitmap.CompressFormat.JPEG, 100, out); 
        out.close(); 
        b.recycle(); 
        Intent in1 = new Intent(this, ongallery.class); 
        in1.putExtra("picture", filename123); 
        startActivity(in1); 
       }catch(Exception e) 
       { 
        Toast.makeText(this,"You got Error!",Toast.LENGTH_SHORT).show(); 
       } 
      } 
     } catch (Exception e) { 
      Toast.makeText(this,"Something went wrong",Toast.LENGTH_SHORT).show(); 
     } 
    } 


//OnaGallery.java 
public class ongallery extends Activity { 
    public ImageView imgView; 
    int xDim; 
    int yDim; 
String filename; 
    public Bitmap finale = null ; 
    public Bitmap bmp = null; 
    public Bitmap photoEdit; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.ongallery); 
     imgView = (ImageView) findViewById(R.id.imgView); 
     xDim = imgView.getWidth(); 
     filename = getIntent().getStringExtra("picture"); 
     try { 
      FileInputStream is = this.openFileInput(filename); 
      bmp = BitmapFactory.decodeStream(is); 
      is.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     Bitmap finalPhoto = decoder(filename,400,400); 
      imgView.setImageBitmap(finalPhoto); 
      } 
    @Override 
    public void onWindowFocusChanged(boolean hasFocus) { 
     super.onWindowFocusChanged(hasFocus); 
    } 
    public Bitmap decoder(String filename, int reqWidth, int reqHeight) { 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     String filepath = getFileStreamPath(filename).getPath(); 
     BitmapFactory.decodeFile(filepath, options); 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 
     options.inJustDecodeBounds = false; 
     finale = BitmapFactory.decodeFile(filepath, options); 
     return finale; 
    } 
    int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { 
     int inSampleSize = 1; 
     if (options.outHeight > reqHeight || options.outWidth > reqWidth) { 
      final int halfHeight = options.outHeight/2; 
      final int halfWidth = options.outWidth/2; 
      while ((halfHeight/inSampleSize) > reqHeight 
        && (halfWidth/inSampleSize) > reqWidth) { 
       inSampleSize *= 2; 
      } 
     } 
     return inSampleSize; 
    } 
} 
+0

是什么ongallary.class? – Nishith

+0

它是包含ImageView的Activity类,我将添加图像。 –

+0

你有没有试过调试你的代码,知道你创建了去你的ongallary活动的意图之后会去哪里? – Nishith

回答

2

类许可相机权限检查

public class Utility { 
public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123; 
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
public static boolean checkPermission(final Context context) 
{ 
    int currentAPIVersion = Build.VERSION.SDK_INT; 
    if(currentAPIVersion>=android.os.Build.VERSION_CODES.N) 
    { 
     if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { 
      if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE)) { 
       AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context); 
       alertBuilder.setCancelable(true); 
       alertBuilder.setTitle("Permission necessary"); 
       alertBuilder.setMessage("External storage permission is necessary"); 
       alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
        public void onClick(DialogInterface dialog, int which) { 
         ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); 
        } 
       }); 
       AlertDialog alert = alertBuilder.create(); 
       alert.show(); 
      } else { 
       ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); 
      } 
      return false; 
     } else { 
      return true; 
     } 
    } else { 
     return true; 
    } 
} 

}

现在检查许可你开除相机意图

​​

现在onActivityResult(前)的样子

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     if (requestCode == CAMERA_REQUEST) 
      bitmap = (Bitmap) data.getExtras().get("data"); 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 

    File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg"); 

    FileOutputStream fo; 
    try { 
     destination.createNewFile(); 
     fo = new FileOutputStream(destination); 
     fo.write(bytes.toByteArray()); 
     fo.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    bitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, false); 
    // Toast.makeText(SignUpActivity.this, String.valueOf(destination), Toast.LENGTH_LONG).show(); 
    imageView.setImageBitmap(bitmap); 
    } else if (resultCode == RESULT_CANCELED) { 
     Toast.makeText(getActivity(), "Cancelled", 
       Toast.LENGTH_SHORT).show(); 
    } 

} 

权限在AndroidMenifest.xml

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

希望这会帮助你。

+0

我用这个方法。但是在这行代码中: bitmap =(位图)data.getExtras()。get(“data”); 这将只获得图像的缩略图而不是全尺寸的图像。因此,imageView将显示非常小的照片(缩略图),我试图避免。 –

+0

使位图成为glbal字段 –

1

如果您在运行Android代码> = 6你需要运行它,并安装前后授予权限,以您的应用程序从您的设备?你做了那个这可以通过应用程序信息/设置菜单完成。

转到

设置 - >应用程序 - >(选择您的应用程序) - >权限 - >(启用所有权限)

+0

我在Android设备上使用Android版本5.2.2测试了此应用程序 –

+0

然后,它可能与图像显示在您的ongallary活动中有关。你有没有尝试过它的debigging? – Nishith

+0

是的,我已经做到了。我还在问题中添加了ongallery.java代码。你可以检查出来。 –