Question

Tried many methods, but did not reach a positive outcome. In TableLayout has ImageView:

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/photo_layout_background"
    android:stretchColumns="*" >

    <TableRow
        android:background="@color/photo_layoutrow_background"
        android:layout_weight="1"
        android:layout_marginBottom="4dp"
        android:gravity="center" >

    <ImageView
        android:id="@+id/photo1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="4dp"
        android:src="@drawable/icon_photo"
        android:background="@color/photo_background"
        android:clickable="true"
        android:onClick="addPhoto_Click"/>

When you click on ImageView offer standard procedure for obtaining pictures and the file is saved on the SD card:

public void addPhoto_Click(View v){
    select_add_photo_id = v.getId();
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, generateFilePhotoUri());
    startActivityForResult(takePictureIntent, CAMERA_RESULT);
}

@SuppressLint("SimpleDateFormat")
private Uri generateFilePhotoUri() {
    File file = null;
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    file = new File(MEDIA_PATH + String.valueOf(id) + File.separator + timeStamp + ".jpg");
    select_add_photo_file = file.getAbsolutePath();
    return Uri.fromFile(file);
}   

The photo is saved normally stored in variables select_add_photo_id id ImageView on which tapped the select_add_photo_file variable holds the full path to the file photo.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_RESULT) {
        if (resultCode == RESULT_OK){
            ImageView imgView = (ImageView) findViewById(select_add_photo_id);
            imgView.setImageDrawable(Drawable.createFromPath(select_add_photo_file));

        }

    }
}   

But when the file is only get ImageView ImageView with background and increased size. Photography is not loaded. There are no errors.

As still set photos?

Was it helpful?

Solution

after take a phone, in onActivityResult, data.getData may be null, in this case, try data.getExtras()

Bitmap photo = null;  
Uri photoUri = data.getData();  
String filePath = "";
if (photoUri != null) {  
    //mFilePath = getRealPathFromURI(photoUri);
    filePath = getRealPathFromURI(photoUri);
    photo = BitmapFactory.decodeFile(photoUri.getPath());  
}  
if (photo == null) {  
    Bundle extra = data.getExtras();  
    if (extra != null) {  
         photo = (Bitmap)extra.get("data");
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top