2016-11-24 146 views
0

我不知道爲什麼,但是我拍攝的圖像不會顯示在我的圖像視圖上。如果我嘗試顯示已經在我的應用中的圖像(例如,在mipmap文件夾中),它將顯示該圖像,但它不會顯示我拍攝的圖像。我的代碼有問題嗎?爲什麼我的照片不會顯示在我的圖像視圖上

private File imageFile; 
private static final int PHOTO_TAKEN = 0; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_photo_taker); 

    addSnapButtonListener(); 
} 

private void addSnapButtonListener(){ 
    Button snapBtn = (Button) findViewById(R.id.camera_button); 

    snapBtn.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v) { 

      File picturesDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
      imageFile = new File(picturesDirectory, "passpoints_image"); 

      Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile)); 
      startActivityForResult(i, PHOTO_TAKEN); 
     } 
    }); 
} 

protected void onActivityResult(int reqC, int resC, Intent data) { 

    if(reqC == PHOTO_TAKEN){ 
     // Bitmap photo = BitmapFactory.decodeFile(imageFile.getAbsolutePath()); 
     Bitmap photo = (Bitmap) data.getExtras().get(imageFile.getAbsolutePath()); 
    // Bitmap photo = (Bitmap) data.getExtras().get("data"); 
     if(photo != null){ 
      ImageView imageView = (ImageView)findViewById(R.id.taken_image); 
      imageView.setImageBitmap(photo); 
     } else { 
      Toast.makeText(this, R.string.unable_to_set_photo_file, Toast.LENGTH_LONG).show(); 
     } 
    } 

} 

我嘗試了幾個不同的方法來讓我的拍攝的圖像(因爲你可以用註釋掉的代碼中看到的),但每當我拍照,我看它的ImageView尚屬空白。

當我嘗試從圖庫中顯示圖像時,也會發生這種情況。我的手機有問題嗎?

編輯:此外,圖像路徑確實得到註冊,因爲我可以檢索它並將其顯示在吐司或日誌中,所以我不認爲這是問題的一部分。

+0

您是否確認在您的活動中一切正常?嘗試添加日誌。如果你的Bitmap被返回爲null,那麼當你設置它時,它不會在視圖中顯示任何東西。但要消除這種可能性,請使用其他設備。 – JoxTraex

回答

1
Bitmap photo = (Bitmap) data.getExtras().get(imageFile.getAbsolutePath()); 

這將返回null,因爲沒有額外的名稱。

此代碼,您已經註釋掉,接近:

Bitmap photo = BitmapFactory.decodeFile(imageFile.getAbsolutePath()); 

但是,你需要保留imageFile,爲您的活動可能會被破壞或將相機的應用程序是在你的進程可能會被終止前景。而且,Uri.fromFile()在您將targetSdkVersion移至24或更高時,將無法在Android 7.0及更高版本的設備上使用。另外,請勿在主應用程序線程上的BitmapFactory上調用decode...(),因爲當磁盤I/O和計算正在進行時,您的UI將凍結。

This sample app(從my book,FWIW),展示了使用FileProvider代替Uri.fromFile()和保持使用onSaveInstanceState()的路徑。就我而言,我通過展示的ACTION_VIEW圖像,而不是裝載到一個ImageView

/*** 
Copyright (c) 2008-2016 CommonsWare, LLC 
Licensed under the Apache License, Version 2.0 (the "License"); you may not 
use this file except in compliance with the License. You may obtain a copy 
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required 
by applicable law or agreed to in writing, software distributed under the 
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
OF ANY KIND, either express or implied. See the License for the specific 
language governing permissions and limitations under the License. 

From _The Busy Coder's Guide to Android Development_ 
https://commonsware.com/Android 
*/ 

package com.commonsware.android.camcon; 

import android.app.Activity; 
import android.content.ClipData; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.content.pm.ResolveInfo; 
import android.net.Uri; 
import android.os.Build; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.support.v4.content.FileProvider; 
import java.io.File; 
import java.util.List; 

public class CameraContentDemoActivity extends Activity { 
    private static final String EXTRA_FILENAME= 
    "com.commonsware.android.camcon.EXTRA_FILENAME"; 
    private static final String FILENAME="CameraContentDemo.jpeg"; 
    private static final int CONTENT_REQUEST=1337; 
    private static final String AUTHORITY= 
    BuildConfig.APPLICATION_ID+".provider"; 
    private static final String PHOTOS="photos"; 
    private File output=null; 
    private Uri outputUri=null; 

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

    Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

    if (savedInstanceState==null) { 
     output=new File(new File(getFilesDir(), PHOTOS), FILENAME); 

     if (output.exists()) { 
     output.delete(); 
     } 
     else { 
     output.getParentFile().mkdirs(); 
     } 
    } 
    else { 
     output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME); 
    } 

    outputUri=FileProvider.getUriForFile(this, AUTHORITY, output); 

    if (savedInstanceState==null) { 
     i.putExtra(MediaStore.EXTRA_OUTPUT, outputUri); 

     if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) { 
     i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); 
     } 
     else if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) { 
     ClipData clip= 
      ClipData.newUri(getContentResolver(), "A photo", outputUri); 

     i.setClipData(clip); 
     i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); 
     } 
     else { 
     List<ResolveInfo> resInfoList= 
      getPackageManager() 
      .queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY); 

     for (ResolveInfo resolveInfo : resInfoList) { 
      String packageName = resolveInfo.activityInfo.packageName; 
      grantUriPermission(packageName, outputUri, 
      Intent.FLAG_GRANT_WRITE_URI_PERMISSION); 
     } 
     } 

     startActivityForResult(i, CONTENT_REQUEST); 
    } 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 

    outState.putSerializable(EXTRA_FILENAME, output); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, 
            Intent data) { 
    if (requestCode == CONTENT_REQUEST) { 
     if (resultCode == RESULT_OK) { 
     Intent i=new Intent(Intent.ACTION_VIEW); 

     i.setDataAndType(outputUri, "image/jpeg"); 
     i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
     startActivity(i); 
     finish(); 
     } 
    } 
    } 
} 

如果我要加載此爲ImageView,我會用an image-loading libraryPicasso

相關問題