0

im just new in programming. i just want my capture image to be placed instead of the drawable image in second activity. I need help for the imagePath.. codes below

MainActivity

    Button btn_cam = (Button) findViewById(R.id.btn_cam);
    btn_cam.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            file = new File(MainActivity.this.getExternalCacheDir(),
                    String.valueOf(System.currentTimeMillis()) + ".jpg");
            fileUri = Uri.fromFile(file);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
            MainActivity.this.startActivityForResult(intent, CAMERA_PIC_REQUEST);

        }
    });                                                                     }                                                                       
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) { 
            fileUri = data.getData();
            Intent cam = new Intent(this, MainCam.class);
            cam.putExtra("flostic", fileUri.toString());
            startActivity(cam);
            this.finish();
        }

Second Activity

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bitmap = BitmapFactory.decodeFile(image_path, options);
            view.setImageBitmap(bitmap);

        }
Nash
  • 1
  • 2
  • What is the problem? Do you get any exception or code just doesn't work? Please provide code in which you are getting an Intent in second activity. – jelic98 Jul 15 '17 at 14:33
  • i want to know how to get the imagepath – Nash Jul 15 '17 at 14:35
  • mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image).copy(Bitmap.Config.ARGB_8888, true); this is the code that i want to replace with camera image instead of drawable – Nash Jul 15 '17 at 14:37

2 Answers2

0

Based on your comment, you want to get image path in second activity. Here is the crucial line for that.

String image_path = getIntent().getStringExtra("flostic");

Below is the full code.

First activity:

Button btnCam = (Button) findViewById(R.id.btn_cam);
btnCam.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        File file = new File(Environment.getExternalStorageDirectory(),
                String.valueOf(System.currentTimeMillis()) + ".jpg");
        Uri fileUri = Uri.fromFile(file);

        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        i.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        startActivityForResult(i, CAMERA_PIC_REQUEST);
    }
});                                                                             

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

    if(requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) { 
        Intent cam = new Intent(this, MainCam.class);
        cam.putExtra("flostic", data.getDate().toString());
        startActivity(cam);
        finish();
    }
}

Second Activity:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout):

    ImageView view = (ImageView) findView(R.id.your_image_view);

    Intent i = getIntent();
    String imagePath = i.getStringExtra("flostic");

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);

    view.setImageBitmap(bitmap);
}
jelic98
  • 723
  • 1
  • 12
  • 28
  • I add this Intent getIntent = getIntent(); still dont work – Nash Jul 15 '17 at 14:42
  • You are passing image path from first to second activity using Intent and you have to extract image path from Intent object in second activity. Please take a look at this answer: https://stackoverflow.com/questions/6707900/pass-a-string-from-one-activity-to-another-activity-in-android – jelic98 Jul 15 '17 at 14:44
  • can you give me a sample solution?please. – Nash Jul 15 '17 at 14:54
  • i have solved the problem. i use intent to call the image. lol. but it works for now. thank you for your time – Nash Jul 17 '17 at 08:34
0

First add these permissions in manifest

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

If your android version is greater than lolipop then add run time permission to you app. See Documentation here

MainActivity

Put onClickListener() to your camera button

 btn_cam.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, 1);
        }
    });

And then in onActivityResult() get image path from selected image URI like this

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

    Uri uri=data.getData();
    switch (requestCode){


        case 1:
            if (resultCode==RESULT_OK){

                String path=getRealPathFromURI(uri);

                Toast.makeText(this, ""+path, Toast.LENGTH_SHORT).show();

                Intent intent=new Intent(this,SecondActivity.class);
                intent.putExtra("imagepath",path);
                startActivity(intent);
            }
    }
}

Include getRealPathFromURI() method which return path of image taken via camera. Send the image path to second activity using intent.putExtra().

   public String getRealPathFromURI(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    @SuppressWarnings("deprecation")
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

This code is for SecondActivity

And then in your second activity get data(image path) from first activity and store it in string variable and create bitmap from that image path and set bitmap image to your imageview

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_next);

    String imagepath=getIntent().getExtras().getString("imagepath");


    File image_path=new File(imagepath);
    if (image_path.exists()){

        Bitmap imageBitmap = BitmapFactory.decodeFile(image_path.getAbsolutePath());

        ImageView imageView = (ImageView) findViewById(R.id.imageselect);

        imageView.setImageBitmap(imageBitmap);
    }

} }

This is how you can display camera image in second activity taken in first activity

Mohammed Farhan
  • 1,120
  • 8
  • 14
  • after capturing the application crash – Nash Jul 15 '17 at 18:00
  • Have you given camera and write_external_storage permission in manifest? This is working code and I has tested it again before posting here. If its crashing then you are missing something. If you are running app on marshmellow then give permission to your app like this Settings>Applications>select your application>Scroll down and you will permissions click it> switch the permissions required (note: to do this you must also have included permissions in your android manifest first). If its still crashing then post logcat. – Mohammed Farhan Jul 16 '17 at 04:48
  • can u post activity code where this error is getting invoked?? – Mohammed Farhan Jul 17 '17 at 06:47
  • do i need to resize my image? – Nash Jul 17 '17 at 07:55
  • Hey am sorry I was unable to resolve your issue due to busy schedule of work. If my answer has solved your problem then please mark it as answered or upvote it. Thanks. – Mohammed Farhan Jul 17 '17 at 09:31