所以我有一个有很多页面的功能应用程序,这是注册2页面,应该打开一个标准的Android相机,然后使用注册java页面底部的代码存储它。
这是我的代码:
package com.example.mockups;
import android.graphics.drawable.Drawable;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import java.io.File;
public class Register2 extends AppCompatActivity {
Button button;
ImageView imageView;
static final int CAM_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register2);
button = (Button) findViewById(R.id.buttonContinue);
imageView = (ImageView) findViewById(R.id.imageView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent camera_intent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
File file = getFile();
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri. fromFile(file));
startActivityForResult(camera_intent, CAM_REQUEST);
}
});
}
private File getFile()
{
File folder = new File("sdcard/camera");
if (!folder.exists())
{
folder.mkdir();
}
File image_file = new File(folder, "cam_image.jpg");
return image_file;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String path = "sdcard/camera/cam_image.jpg";
imageView.setImageDrawable(Drawable.createFromPath(path));
}
}
_________________________________
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_register2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.strongholdmockups.Register2">
<TextView
android:text="Build Profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buildProTitle"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:text="Upload Profile Picture:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="33dp"
android:id="@+id/textView46"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:layout_below="@+id/buildProTitle"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="36dp"
android:layout_marginStart="36dp" />
<Button
android:text="Upload from Camera Roll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="21dp"
android:id="@+id/buttonContinue"
style="@android:style/Widget.Holo.Light.Button"
android:layout_below="@+id/textView46"
android:layout_alignRight="@+id/buildProTitle"
android:layout_alignEnd="@+id/buildProTitle" />
<ImageView
android:id="@+id/image_view"
android:layout_width="350dp"
android:layout_height="250dp"
android:layout_above="@+id/continueBtn1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="center_horizontal" />
<Button
android:text="Continue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="23dp"
android:layout_marginEnd="23dp"
android:layout_marginBottom="14dp"
android:id="@+id/continueBtn1"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:background="@color/cardview_light_background"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/image_view"
android:layout_alignEnd="@+id/image_view" />
</RelativeLayout>
__________________
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
答案 0 :(得分:0)
您必须检查是否为API版本授予了权限&gt; = 23.在使用相机/外部存储设备之前,请在您的代码中添加此类内容。
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
} else {
Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
return false;
}
}else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
return true;
}