Pergunta

I have a application to capture photo which will display on ImageView (and do some more work internally).

Problem is here when I open camera and after capturing photo, save and discard option come, after clicking on save button it again go to the camera mode.

This problem not occur when I clean the memory of phone(Task Manager ->> Ram ->> Clear Memory).

I thing the problem is: Camera Intent not give result when there is not sufficient memory. But when I open normal Camera then it capture the photo.

How I solve this problem. One solution is here every time clean memory before launching my application.

But this is not a right way because when I introduce my application to general people then they do not do these thing.

Now what I need to do here?

Is there any way to clean the memory by my application? How I know what the minimum or maximum memory is utilized by my application. So first my application check, is there, that much memory available or not. If available then proceed otherwise tell user to clean(or uninstall some application) some memory.

I am using following code in my application:

layout->

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button 
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="@string/photo"
    />

<ImageView 
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/button1"
    android:contentDescription="@string/image"/>
</RelativeLayout>

activity->>

public class TestCameraActivity extends Activity {

private Button button;
private ImageView imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_camera);

    button = (Button) findViewById(R.id.button1);
    imageView = (ImageView) findViewById(R.id.image);

    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, 100);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 100) {
        Bitmap bitmap = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(bitmap);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_test_camera, menu);
    return true;
}

}
Foi útil?

Solução 2

May be you are not properly calling CameraIntent, try below code, hope it works:

CameraIntent

String filepath = Environment.getExternalStorageDirectory()+"/foldername/"+filename;
System.out.println("thumbnail path~~~~~~"+filepath);
File file = new File(filepath);
Uri outputFileUri = Uri.fromFile(file);

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 100);

onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
        Bitmap bitmap;
        bitmap=GlobalMethods.decodeFile(_path);//function to decode.
        if (bitmap == null) {
            ivPhy.setImageBitmap(bitmap);
        } 
        else {
            ivPhy.setImageBitmap(bitmap);
            ivPhy.setScaleType(ScaleType.FIT_XY);
        }
    }


}

Manifest Permission

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

Outras dicas

Your code is looking any way try below code which worked for me

File root = new File(Environment.getExternalStorageDirectory(), "Directory");
        if (!root.exists()) {
            root.mkdirs();
        }
         File file = new File(root,"filename.jpeg" );
         outputFileUri = Uri.fromFile( file );

         Intent photoPickerIntent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
    photoPickerIntent.putExtra("return-data", true);
   startActivityForResult(photoPickerIntent,100);

OnActivityResult:

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

    switch(requestCode)
     { 

     case 100:
     if(resultCode == RESULT_OK)
     { 

           if(data==null)
            {
               Bitmap bitmap = (Bitmap) data.getExtras().get("data");
                         imageView.setImageBitmap(bitmap);
            }
         break;
     }
}
}

To those who are still searching solution for this issue:

lack of permission is one of the major reasons behind this problem. Please add this permission in your manifest file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top