1

Here, I want To take picture and save it on sd card and get path via button. That the picture path can see into TextView. how can i do it?

public class MainActivity extends AppCompatActivity {

private static final int CAMERA_PIC_REQUEST = 1111;
private ImageView mImage;
Button camera;
Button path;
TextView tvPath;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mImage = (ImageView) findViewById(R.id.camera_image);
    path = (Button) findViewById(R.id.btnPath);
    tvPath  = (TextView) findViewById(R.id.tvPath);

    //1
    camera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, CAMERA_PIC_REQUEST);
        }
    });

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST) {

        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        mImage.setImageBitmap(thumbnail);

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

        File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
        try {
            file.createNewFile();
            FileOutputStream fo = new FileOutputStream(file);

            fo.write(bytes.toByteArray());
            fo.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Sorry for English.

soraya
  • 11
  • 5

1 Answers1

0
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST) {
        Uri filePath = data.getData();
        String fullFilePath = FilePath.getPath(this, filePath);
        if (fullFilePath != null) {
            String filename = fullFilePath.substring(fullFilePath.lastIndexOf("/") + 1);
            tvFilename.setText(filename);
        }
    }
gStephin
  • 332
  • 3
  • 20
  • Thnk you. Could you explain please this line ==> String fullFilePath = FilePath.getPath(this, filePath); – soraya Aug 07 '19 at 06:28