由于java.io.FileNotFoundException,应用程序无法正常工作

时间:2016-05-30 10:00:57

标签: java android android-camera android-permissions android-file

我正在努力从图库中选择图像或从相机中捕捉照片并在RecyclerView中显示。它正在我的模拟器和其他两个Android手机上工作但不在我的平板电脑(Lenovo Phab Plus)上,一旦相机点击图片就会显示以下内容:

cannot compute fingerprint for: content://media/external/images/media/290 java.io.FileNotFoundException: No such file or directory

以下是代码:

MainActivity.java

public class MainActivity extends AppCompatActivity {
private ArrayList<Images> images;
private ImageAdapter imageAdapter;
private RecyclerView rv;
private LinearLayoutManager llm;
private Uri mCapturedImageURI;
private static final int RESULT_LOAD_IMAGE = 1;
private static final int REQUEST_IMAGE_CAPTURE = 2;
private static final int REQUEST_WRITE_STORAGE=3;
static final int PERMISSION_REQUEST_CODE=100;
final static String TAG="";
/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getCameraPermission();
    images = new ArrayList<>();
    imageAdapter = new ImageAdapter(images);
    rv = (RecyclerView) findViewById(R.id.rv);
    llm = new LinearLayoutManager(this);
    rv.setLayoutManager(llm);
    imageAdapter = new ImageAdapter(images);
    rv.setAdapter(imageAdapter);
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}


public void activeCamera(View view) {

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        String fileName = "temp.jpg";
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, fileName);

        mCapturedImageURI = getContentResolver()
                .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        values);
        takePictureIntent
                .putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);

    }
}

 public void getCameraPermission(){
    if (!checkPermission()) {
        requestPermission();
    }
}
private boolean checkPermission(){
    int result = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);

    if (result == PackageManager.PERMISSION_GRANTED){
        return true;
    } else {
        return false;
    }
} private void requestPermission(){

    if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.CAMERA)){

        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE);
    } else {

        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(MainActivity.this,"Permission granted",Toast.LENGTH_SHORT).show();
                //store permission in shared pref

            }

            else {
                Toast.makeText(MainActivity.this,"Permission denied",Toast.LENGTH_SHORT).show();
                //store permission in shared pref
            }
            break;
    }
}

public void activeGallery(View view) {
    Intent intent = new Intent(Intent.ACTION_PICK,
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, RESULT_LOAD_IMAGE);}



@Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case RESULT_LOAD_IMAGE:

            Uri selectedImage = null;
            if (requestCode == RESULT_LOAD_IMAGE &&
                    resultCode == RESULT_OK && null != data) {

                if (Build.VERSION.SDK_INT < 19) {
                    selectedImage = data.getData();
                } else {
                    selectedImage = data.getData();
                }

                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver()
                        .query(selectedImage, filePathColumn, null, null,
                                null);
                if (cursor != null && cursor.getCount() > 0) {
                    cursor.moveToFirst();
                }

                // cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();
                Images image = new Images();
                image.setPath(picturePath);
                images.add(image);
            }
        case REQUEST_IMAGE_CAPTURE:
            if (requestCode == REQUEST_IMAGE_CAPTURE &&
                    resultCode == RESULT_OK) {

                String[] projection = {MediaStore.Images.Media.DATA};
                Cursor cursor =
                        getContentResolver().query(mCapturedImageURI, projection, null,
                                null, null);
                int column_index_data = cursor.getColumnIndexOrThrow(
                        MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                String picturePath = cursor.getString(column_index_data);
                Images image = new Images();
                image.setPath(picturePath);
                images.add(image);
                cursor.close();
            }
    }
}}

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.urjapawar.bevypart2">
<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"
    android:required="false"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
   >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
 App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
</application>

</manifest>

ImagesAdapter.java

public class ImageAdapter extends android.support.v7.widget.RecyclerView.Adapter<ImageAdapter.ImgViewHolder> {
List<Images> images;
ImageAdapter(List<Images> img){
    this.images = img;
}
public  class ImgViewHolder extends android.support.v7.widget.RecyclerView.ViewHolder
{
    ImageView personPhoto;
    CardView cv;
    public ImgViewHolder(View itemView) {
        super(itemView);
        cv = (CardView)itemView.findViewById(R.id.card_view);
        personPhoto=(ImageView)itemView.findViewById(R.id.thumbnail);
    }
}

@Override
public ImgViewHolder onCreateViewHolder(final ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.single_card, viewGroup, false);
    ImgViewHolder pvh = new ImgViewHolder(v);


    return pvh;
}

@Override
public int getItemCount() {
    return images.size();
}
@Override
public void onBindViewHolder(ImgViewHolder holder, int i) {
    Images image = images.get(i);
    final int THUMBSIZE = 96;
    holder.personPhoto.setImageBitmap(ThumbnailUtils
            .extractThumbnail(BitmapFactory.decodeFile(image.getPath()),
                    THUMBSIZE, THUMBSIZE));

}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

}

这里是logcat

05-30 07:02:40.951 27858-1310/? I/iu.FingerprintManager: Start processing media store URI: content://media/external/images/media
05-30 07:02:40.956 883-9613/? V/BroadcastQueue: processNextBroadcast() intent[Intent { act=android.intent.action.ACTION_POWER_CONNECTED flg=0x4000010 } pkg[com.google.android.apps.photos] cls[com.google.android.apps.photos.dbprocessor.impl.DatabaseProcessorReceiver]
05-30 07:02:40.956 883-9613/? V/BroadcastQueue: processNextBroadcast() boot_complete op mode:0
05-30 07:02:40.962 883-1560/? V/BroadcastQueue: processNextBroadcast() intent[Intent { act=android.intent.action.ACTION_POWER_CONNECTED flg=0x4000010 } pkg[com.google.android.apps.photos] cls[com.google.android.apps.photos.scheduler.PowerReceiver]
05-30 07:02:40.962 883-1560/? V/BroadcastQueue: processNextBroadcast() boot_complete op mode:0
05-30 07:02:40.971 883-2192/? V/BroadcastQueue: processNextBroadcast() intent[Intent { act=android.intent.action.ACTION_POWER_CONNECTED flg=0x4000010 } pkg[com.google.android.apps.photos] cls[com.google.android.apps.photos.trash.purger.PurgeTrashRegisterReceiver]
05-30 07:02:40.972 883-2192/? V/BroadcastQueue: processNextBroadcast() boot_complete op mode:0
05-30 07:02:40.981 883-16462/? V/BroadcastQueue: processNextBroadcast() intent[Intent { act=android.hardware.usb.action.USB_STATE flg=0x20000010 (has extras) } pkg[com.android.providers.media] cls[com.android.providers.media.MtpReceiver]
05-30 07:02:40.981 883-16462/? V/BroadcastQueue: processNextBroadcast() boot_complete op mode:0
05-30 07:02:40.990 27858-1310/? E/LocalFingerprints: cannot compute fingerprint for: content://media/external/images/media/290            java.io.FileNotFoundException: No such file or directory
                                                     at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
                                                     at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:689)
                                                     at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1080)
                                                     at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:921)
                                                     at android.content.ContentResolver.openInputStream(ContentResolver.java:646)
                                                     at acmh.a(SourceFile:174)
                                                     at acks.a(SourceFile:138)
                                                     at com.google.android.libraries.social.autobackup.FingerprintScannerChimeraIntentService.onHandleIntent(SourceFile:66)
                                                     at beq.handleMessage(SourceFile:65)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:135)
                                                     at android.os.HandlerThread.run(HandlerThread.java:61)
05-30 07:02:40.990 27858-1310/? I/iu.FingerprintManager: Not inserting fingerprint into all photos because has empty content uri or fingerprint. uri: true fingerprint: false
05-30 07:02:40.997 27858-1310/? I/iu.FingerprintManager: Start processing media store URI: content://media/external/video/media
05-30 07:02:41.002 976-1311/? E/LocalFingerprints: cannot compute fingerprint for: content://media/external/images/media/290

请帮忙!

0 个答案:

没有答案