2016-10-26 13 views
-1

Iveはピクチャを取得し、対応するピクチャをファイルプロバイダを使用して画面上のImageViewにロードするように単純なフラグメントを設計しました。私は現在、それがそこにあり、正しい見出しの下にあるにもかかわらず、それにアクセスするときにnullポインタ例外を取得しています。私はすべての設定をチェックし、それらが一致していることを確認しましたが、アプリケーションが自分のファイルプロバイダを見つけることができません。ファイルプロバイダのヌルポインタ例外

import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.media.Image; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.support.design.widget.FloatingActionButton; 
import android.support.v4.app.Fragment; 
import android.support.v4.content.FileProvider; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import java.io.File; 
import java.util.Date; 
import android.net.Uri; 
import android.widget.ImageView; 
import java.io.IOException; 
import java.text.SimpleDateFormat; 

/** 
* A simple {@link Fragment} subclass. 
* Use the {@link StudentID#newInstance} factory method to 
* create an instance of this fragment. 
*/ 
public class StudentID extends Fragment { 
    String mCurrentPhotoPath; 
    Uri mPhotoURI; 
    static final int REQUEST_IMAGE_CAPTURE = 1; 
    static final int REQUEST_TAKE_PHOTO = 1; 
    ImageView picView; 
    FloatingActionButton button; 
    public StudentID() { 
     // Required empty public constructor 
    } 

    /** 
    * Use this factory method to create a new instance of 
    * this fragment using the provided parameters. 
    * 
    * @return A new instance of fragment StudentID. 
    */ 
    // TODO: Rename and change types and number of parameters 
    public static StudentID newInstance() { 
     StudentID fragment = new StudentID(); 
     Bundle args = new Bundle(); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.fragment_student_id, container, false); 
     picView = (ImageView)view.findViewById(R.id.idPic); 
     button = (FloatingActionButton)view.findViewById(R.id.picButton); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       takePicture(); 
      } 
     }); 
     //updateImage(); 
     return view; 
    } 
    public void takePicture() { 
     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     if (takePictureIntent.resolveActivity(getContext().getPackageManager()) != null) { 
      startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); 
     } 
     File photoFile = null; 
     try { 
      photoFile = createImageFile(); 
     } catch (IOException ex) { 
     } 
     // Continue only if the File was successfully created 
     if (photoFile != null) { 
      mPhotoURI = FileProvider.getUriForFile(getContext(), 
        "com.tble.brgo.fileprovider", 
        photoFile); 
      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoURI); 
      startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); 
     } 
    } 
    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == getActivity().RESULT_OK) { 
      picView.setImageURI(mPhotoURI); 
     } 
    } 
    private File createImageFile() throws IOException { 
     // Create an image file name 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     String imageFileName = "JPEG_" + timeStamp + "_"; 
     File storageDir = getContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
     File image = File.createTempFile(
       imageFileName, /* prefix */ 
       ".jpg",   /* suffix */ 
       storageDir  /* directory */ 
     ); 
     mCurrentPhotoPath = "file:" + image.getAbsolutePath(); 
     return image; 
    } 


} 

以下の私のコードのAndroidマニフェスト

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.tble.brgo"> 
    <provider 
     android:name="android.support.v4.content.FileProvider" 
     android:authorities="com.tble.brgo.fileprovider" 
     android:exported="false" 
     android:grantUriPermissions="true"> 
     <meta-data 
      android:name="android.support.FILE_PROVIDER_PATHS" 
      android:resource="@xml/file_paths.xml"></meta-data> 
    </provider> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-feature android:name="android.hardware.camera" android:required="false" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
     android:maxSdkVersion="18"/> 

FileProvider:

<?xml version="1.0" encoding="utf-8"?> 
<paths xmnls="http://schemas.android.com/apk/res/android"> 
    <external-path name="my_images" path="Android/data/com.tble.brgo.BRGO/files/Pictures" /> 
</paths> 

答えて

0

私は、このコードの小片を使用してこのタスクをしました。

String imageName = String.valueOf(System.currentTimeMillis()); 
       filename = Environment.getExternalStorageDirectory().getPath() + "/YourFolder/" + imageName + ".jpg"; 
       imageUri = Uri.fromFile(new File(filename)); 
       // start default camera 
       Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, 
         imageUri); 
       startActivityForResult(cameraIntent, 100); 

onActivityResult

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(requestCode != 100 || filename == null) 
     return; 
    bitmap = BitmapFactory.decodeFile(imageUri.getPath()); //set bitmap to your imageview. 

} 
0

すでに余分にファイルパスを設定する前にアクションイメージキャプチャテントを開始しました。

ここにコードの変更があります。

public void takePicture() { 
     File photoFile = null; 
     try { 
      photoFile = createImageFile(); 
     } catch (IOException ex) { 
     } 
     // Continue only if the File was successfully created 
     if (photoFile != null) { 
      mPhotoURI = FileProvider.getUriForFile(getContext(), 
        "com.tble.brgo.fileprovider", 
        photoFile); 
      Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      if (takePictureIntent.resolveActivity(getContext().getPackageManager()) != null) { 
       takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoURI); 
       startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); 
      } 
     } 
    } 
+0

数日前とまったく同じことをしましたが、ここでサンプルを参照できます。https://github.com/santbob/AppPrivateImageUpload/ – SanthoshN

0

あなたは、あなたがファイル・プロバイダのXMLファイルにexternal-files-path代わりのexternal-pathを使用する必要があり、ファイルを作成するためのgetExternalFilesDir()を使用している場合。