2

我正在尝试在 adroid 上使用 CAMERA 意图来拍照并将其显示在图片视图中。在我拍照之前,下面的代码可以正常工作。然后我无法使用“V”按钮确认并返回我的活动。如果我单击“X”按钮,android 将返回我的活动,但没有照片,但如果我单击“V”确认图片,则没有任何反应,我仍然卡在相机屏幕中。

我正在使用 API 14 并使用 android 4.0.2 在虚拟和物理设备上进行测试。

我的错误在哪里?

import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class HandScryActivity extends Activity {

    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
    private Uri uriSavedImage;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.handscry); 
        // Makes the filename
        File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
        imagesFolder.mkdirs();
        File image = new File(imagesFolder, "image_001.jpg");
        uriSavedImage = Uri.fromFile(image);
    }

    // Handles onGame clicked buttons
    public void btnHandClick(View v) {
            Button clickedButton = (Button) v;
        // according to clicked button
        switch (clickedButton.getId()) {
            case R.id.btnBackToGame:
                this.finish();
                break;
            case R.id.btnTakePicture:
                Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);               
                imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
                startActivityForResult(imageIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
                break;              
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        ImageView img = (ImageView) findViewById(R.id.imgHand);
            if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            // Display image
                if (resultCode == RESULT_OK) {
                    img.setImageURI(uriSavedImage);                                
            } else if (resultCode == RESULT_CANCELED) {
                // User cancelled the image capture
            } else {
                // Image capture failed, advise user
            }
        }
    }
}
4

3 回答 3

2

解决方案在这里,以防其他人在我的错误中绊倒。我从一个网站上得到了那个样本,但这部分似乎卡在了 android 上,并且在成功的情况下无法获得“OnActivityResult”:

// Makes the filename
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "image_001.jpg");
uriSavedImage = Uri.fromFile(image);

应该替换为

File image = new File(Environment.getExternalStorageDirectory(), "test.jpg");        
uriSavedImage = Uri.fromFile(image);
于 2012-04-15T10:42:38.130 回答
0

在我的情况下,问题是由于在启动活动时添加了android.provider.MediaStore.EXTRA_OUTPUT使用 4.4.4 版生成的。FileProviderACTION_IMAGE_CAPTURE

当前活动是这样开始的:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

Uri photoURI = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
                    ? FileProvider.getUriForFile(context, context.getPackageName(),photoFile)
                    : Uri.fromFile(photoFile);

// write the captured image to a file
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoURI);

// start it from the activity and not from the dialog so the request code will be the
// one expected and not the one from the dialog
activity.startActivityForResult(intent, CAMERA_REQUEST_CODE);

希望它可以帮助某人。

于 2018-05-02T11:22:07.057 回答
0

我面临着类似的问题。我能够打开相机意图然后捕捉图像,但无法点击一加 6T 上的 OK(勾选)按钮。调试没有帮助。然后我发现这个问题与内存优化有关。我杀死了所有其他后台应用程序并再次尝试并成功了。

有关更多信息,请参阅此链接。选项 2 在此链接中对我有用。

复制粘贴链接中的答案:

"When returning from the camera intent sometimes the app was being destroyed by the operating system (I'm assuming for memory
reasons). This resulted in sporadic behavior and my state object
would have several null fields upon returning, making my app crash.

Fix: Write state object to file before sending picture, load in my parent activity class which handles onCreate then goes to the subclassed activity."
于 2019-03-25T07:44:13.010 回答