在更新我的galaxy tab 2的软件版本之前,我的应用程序工作正常,但在更新到7.0版之后,相机功能无法正常工作。它需要一张图片,显示预览,但在预览页面上按“确定”后崩溃。当我检查画廊时,图像也没有保存。 这是我的代码。
public void btnCameraOnClick(View v)
{
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,CAMERA_REQUEST);
}
清单中的
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
不确定是否有任何错误 这是日志,不确定它是否需要
09-18 11:46:01.520 8795-8795/henderson.application E/AndroidRuntime: FATAL EXCEPTION: main
Process: henderson.application, PID: 8795
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data flg=0x1 launchParam=MultiScreenLaunchParams { mDisplayId=0 mFlags=0 } (has extras) }} to activity {henderson.application/henderson.application.Application_admin_1}: java.lang.NullPointerException: uri
at android.app.ActivityThread.deliverResults(ActivityThread.java:4524)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4567)
at android.app.ActivityThread.-wrap22(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1695)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6780)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Caused by: java.lang.NullPointerException: uri
at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:111)
at android.content.ContentResolver.query(ContentResolver.java:519)
at android.content.ContentResolver.query(ContentResolver.java:478)
at henderson.application.Application_admin_1.onActivityResult(Application_admin_1.java:114)
at android.app.Activity.dispatchActivityResult(Activity.java:7273)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4520)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4567)
at android.app.ActivityThread.-wrap22(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1695)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6780)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
活动代码
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SELECTED_PICTURE:
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null); // line 114
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
image_file_path = cursor.getString(columnIndex);
cursor.close();
File f = new File(image_file_path);
Sagar的日志错误
09-18 13:59:29.393 16404-16404/henderson.application E/AndroidRuntime: FATAL EXCEPTION: main
Process: henderson.application, PID: 16404
android.os.FileUriExposedException: file:///storage/emulated/0/tmp exposed beyond app through ClipData.Item.getUri()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)
at android.net.Uri.checkFileUriExposed(Uri.java:2346)
at android.content.ClipData.prepareToLeaveProcess(ClipData.java:832)
at android.content.Intent.prepareToLeaveProcess(Intent.java:9514)
at android.content.Intent.prepareToLeaveProcess(Intent.java:9499)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1525)
at android.app.Activity.startActivityForResult(Activity.java:4403)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
at android.app.Activity.startActivityForResult(Activity.java:4362)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:871)
at henderson.application.Application_admin_1$1.onClick(Application_admin_1.java:228)
at android.view.View.performClick(View.java:6257)
at android.widget.TextView.performClick(TextView.java:11149)
at android.view.View$PerformClick.run(View.java:23705)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6780)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
错误发生在startActivityForResult(intent,CAMERA_REQUEST);
答案 0 :(得分:0)
因为版本6.0以上需要运行时权限
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.yalantis.ucrop.UCrop;
import java.io.File;
public class MainActivity extends AppCompatActivity {
LinearLayout linsketch,lincamera,linalpha,linsigno;
public static File mFileTemp;
Uri selectedImageUri;
public static final int RESULT_FROM_CAMERA = 1;
public static final int RESULT_FROM_GALLERY = 2;
String pathname;
public static final int RequestPermissionCode = 104;
private static final String SAMPLE_CROPPED_IMAGE_NAME = "SampleCropImage";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mFileTemp = new File(Environment.getExternalStorageDirectory(),
Utils.TEMP_FILE_NAME);
pathname=mFileTemp+"";
Toast.makeText(getApplicationContext(),pathname,Toast.LENGTH_SHORT).show();
} else {
mFileTemp = new File(getFilesDir(), Utils.TEMP_FILE_NAME);
pathname=mFileTemp+"";
}
EnableRuntimePermission();
linsketch=(LinearLayout)findViewById(R.id.linsketch);
lincamera=(LinearLayout)findViewById(R.id.lincamera);
linalpha=(LinearLayout)findViewById(R.id.linalpha);
linsigno=(LinearLayout)findViewById(R.id.linsigno);
linsketch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(1);
dialog.setContentView(R.layout.gallary_or_camera);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
ImageView gal = (ImageView) dialog.findViewById(R.id.gall);
ImageView cam=(ImageView)dialog.findViewById(R.id.cam);
gal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
// Toast.makeText(getApplicationContext(),"you clicked at gallery",Toast.LENGTH_SHORT).show();
pickFromGallery();
}
});
cam.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFileTemp));
startActivityForResult(intent, RESULT_FROM_CAMERA);
}
}
);
dialog.show();
}
});
}
private void EnableRuntimePermission() {
if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE)
+ ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale
(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) ||
ActivityCompat.shouldShowRequestPermissionRationale
(MainActivity.this, android.Manifest.permission.CAMERA)) {
Toast.makeText(MainActivity.this, "Allow permissions to Edit the Image", Toast.LENGTH_LONG).show();
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(
new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.CAMERA}, RequestPermissionCode);
}
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case RequestPermissionCode:
if (grantResults.length > 0) {
boolean cameraPermission = grantResults[1] == PackageManager.PERMISSION_GRANTED;
boolean writeExternalFile = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if (cameraPermission && writeExternalFile) {
} else {
Toast.makeText(MainActivity.this, "Allow permissions to Edit the Image", Toast.LENGTH_LONG).show();
}
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFileTemp));
startActivityForResult(intent, RESULT_FROM_CAMERA);
}
break;
}
}
private void pickFromGallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, getString(R.string.label_select_picture)), RESULT_FROM_GALLERY);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == RESULT_FROM_GALLERY) {
final Uri selectedUri = data.getData();
Log.d("sss",""+selectedUri);
if (selectedUri != null) {
startCropActivity(data.getData());
} else {
Toast.makeText(MainActivity.this, R.string.toast_cannot_retrieve_selected_image, Toast.LENGTH_SHORT).show();
}
}
else if(requestCode==RESULT_FROM_CAMERA){
File imageFile = new File(pathname);
Uri picUri = Uri.fromFile(imageFile);
startCropActivity(picUri);
}else if (requestCode == UCrop.REQUEST_CROP) {
handleCropResult(data);
}
}
}
private void startCropActivity(@NonNull Uri uri) {
String destinationFileName = SAMPLE_CROPPED_IMAGE_NAME;
destinationFileName += ".png";
UCrop uCrop = UCrop.of(uri, Uri.fromFile(new File(getCacheDir(), destinationFileName)));
uCrop.start(MainActivity.this);
}
private void handleCropResult(@NonNull Intent result) {
final Uri resultUri = UCrop.getOutput(result);
if (resultUri != null) {
ResultActivity.startWithUri(MainActivity.this, resultUri);
} else {
Toast.makeText(MainActivity.this, R.string.toast_cannot_retrieve_cropped_image, Toast.LENGTH_SHORT).show();
}
}
}
答案 1 :(得分:0)
将其放在相机上点击代码
agent = Mechanize.new
page = agent.get url
puts page.uri # this will be the redirected url