2016-11-30 17 views
-1
シンプルなコードは以下のイメージ図 上のSDカードとディスプレイからイメージファイルを読むことです

enter image description hereはコードアンドロイドは:画像ビューを与えるエラーにSDカードとディスプレイからファイルを読み込む

import java.io.File; 

import android.support.v7.app.ActionBarActivity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.ImageView; 


public class MainActivity extends ActionBarActivity { 

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

     File imgFile = new File("/storage/extSdCard/DCIM/Camera/Test.jpg"); 

     if(imgFile.exists()){ 

      Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 

      ImageView myImage = (ImageView) findViewById(R.id.imgView); 

      myImage.setImageBitmap(myBitmap); 

     } 
     setContentView(R.layout.activity_main); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

IでありますSamsung S3をアンドロイドデバイスとして使用しています。私が受け取っているエラーは、 "java.Lang.RuntimeException:アクティビティコンポーネントを起動できません"というlogcatです。必要な許可も考慮された。 コードを正しく動作させるための提案が必要です。 (また、アプリは正常にデバイスにインストールされていますが、正しく動作することはできません)。

+2

あなたのlogcatを見せてください – cuoka

+0

@cuoka logcatの画像が追加されました – farhan

答えて

1

このステートメントは:

ImageView myImage = (ImageView) findViewById(R.id.imgView); 

setContentView();

前にmyImageしたがって、私はあなたが取得していると仮定しNullPointerException、nullになりますそれをやった方法を行うことはできません。

+0

ありがとうございました。 – farhan

関連する問題