2017-03-22 82 views
-2

所以我有一個功能的應用程序,有很多頁面,這是註冊2頁,應該打開一個標準的安卓相機,然後存儲它使用註冊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"/> 
+1

請說爲什麼你的應用程序不會運行。你會得到什麼錯誤? –

+0

你的日誌顯示了什麼? – Coeus

回答

0

你必須檢查是否授予權限的API版本> = 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; 
} 
+0

我應該將此代碼添加到Android清單嗎? –

+0

不,請在要使用相機/外部存儲器的活動中添加此代碼。這需要在開始相機活動之前執行。 – Cimoe

相關問題