2016-09-26 91 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代碼。你可以檢查出來。 –