Android の初心者です。img.setImageDrawable(Drawable.createFromPath(file.toString())); を使用して、リスト ビューに内部 (dat/data/)jpg 画像 (sdCard ではない) を表示しようとしています。
私のコードが少し乱雑であることはわかっています。後でリファクタリングしますが、先に進むことができるように、この部分を機能させたいだけです。どうもありがとう。
私は得続けます
07-31 11:27:08.905: D/AndroidRuntime(17470): Shutting down VM
07-31 11:27:08.905: W/dalvikvm(17470): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
07-31 11:27:08.965: E/AndroidRuntime(17470): FATAL EXCEPTION: main
07-31 11:27:08.965: E/AndroidRuntime(17470): java.lang.NullPointerException
07-31 11:27:08.965: E/AndroidRuntime(17470): at com.walkingroutes.beta01.RouteList$1.setViewValue(RouteList.java:142)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.support.v4.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:131)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:256)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.widget.AbsListView.obtainView(AbsListView.java:2271)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.widget.ListView.onMeasure(ListView.java:1156)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.view.View.measure(View.java:15172)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.view.View.measure(View.java:15172)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.view.View.measure(View.java:15172)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.view.View.measure(View.java:15172)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-31 11:27:08.965: E/AndroidRuntime(17470): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.view.View.measure(View.java:15172)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.view.Choreographer.doFrame(Choreographer.java:525)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.os.Handler.handleCallback(Handler.java:615)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.os.Looper.loop(Looper.java:137)
07-31 11:27:08.965: E/AndroidRuntime(17470): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-31 11:27:08.965: E/AndroidRuntime(17470): at java.lang.reflect.Method.invokeNative(Native Method)
07-31 11:27:08.965: E/AndroidRuntime(17470): at java.lang.reflect.Method.invoke(Method.java:511)
07-31 11:27:08.965: E/AndroidRuntime(17470): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-31 11:27:08.965: E/AndroidRuntime(17470): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-31 11:27:08.965: E/AndroidRuntime(17470): at dalvik.system.NativeStart.main(Native Method)
07-31 11:27:09.005: D/dalvikvm(17470): GC_CONCURRENT freed 259K, 3% free 10981K/11271K, paused 21ms+6ms, total 88ms
07-31 11:27:09.005: D/dalvikvm(17470): WAIT_FOR_CONCURRENT_GC blocked 40ms
コード:
public class RouteList extends BaseActivity {
Cursor c;
ProgressDialog dialog;
MainActivity mainAct;
DBAdapter db = new DBAdapter(this);
ArrayList<String> l;
ArrayList<String> l2;
ArrayList<String> row_details;
Cursor cursor;
ListView myList;
public JSONArray getArray() throws JSONException {
db.open();
JSONArray j = new JSONArray(db.readJSONFeed("http://www.example.com/api/all"));
return j;
}
public void onClick2(View v){
}
public Long getCount(){
db.open();
return db.countRoutes();
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.route_list);
Log.d("RouteList","getAllRoutes");
myList = (ListView) findViewById(R.id.route_list);
RouteList.this.setupList();
}//end of onCreate
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
// UNregister the receiver
//unregisterReceiver(receiver);
}
private void setupList(){
TextView text = (TextView) findViewById(R.id.list_title);
Long count = getCount();
text.setText(" " +count + " trails");
db.open();
db.createImagesDB();
db.open();
cursor = db.getAllRoutes();
startManagingCursor(cursor);
String[] fromFieldNames = new String[]{
DBAdapter.KEY_NAME,DBAdapter.C_DISTANCE,DBAdapter.C_COUNTY,"cache"
};
int[] toViewIDs = new int[]{
R.id.route_list_title,R.id.route_list_detail,R.id.route_list_county,R.id.icon
};
//Create an adapter to mount columns with the UI.
SimpleCursorAdapter routeListAdapter = new SimpleCursorAdapter(
RouteList.this,
R.layout.route_list_row,
cursor,
fromFieldNames,
toViewIDs
);
//AMEND THE LIST DATA
routeListAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int column) {
if( column == 4 ){ // let's suppose that the column 0 is the date
ImageView img = (ImageView) findViewById(R.id.icon);
String fn = cursor.getString(cursor.getColumnIndex("cache"));
if(fn != null){
Log.d("routeListAdapter","File name is: "+fn);
String filePath = getApplication().getFilesDir().getAbsolutePath()+ File.separator + fn;
File f = new File(filePath);
if (f.exists()) {
Log.d("routeListAdapter","File does exist!");
Bitmap bitmap = null;
FileInputStream fin = null;
String string = "hello";
try {
fin = openFileInput(fn);
Log.d("routeListAdapter","Got fin");
File file = getBaseContext().getFileStreamPath(fn);
Log.d("routeListAdapter","file://"+file.toString());
img.setImageDrawable(Drawable.createFromPath(file.toString()));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//if f exists
}else{
Log.e("routeListAdapter","File name not found!");
}
return true;
}
if( column == 3 ){
TextView v = (TextView) view;
String distance = cursor.getString(cursor.getColumnIndex("distance"));
distance += " km";
v.setText(distance);
return true;
}
if( column == 2 ){
TextView v = (TextView) view;
String county = cursor.getString(cursor.getColumnIndex("county"));
// county = "County: "+county;
String countyString = (String) county;
countyString = DBAdapter.getCounty(countyString);
v.setText(countyString);
return true;
}
return false;
}
});
myList.setAdapter(routeListAdapter);
// CLICK LISTENER
myList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0,View arg1, int arg2, long id) {
Log.d("onItemClick", String.valueOf(id));
Intent i = new Intent(RouteList.this,DetailActivity.class);
i.putExtra("_id", String.valueOf(id));
startActivity(i);
Log.d("onlistItemClick","activity started");
}
});
if(count <= 0){
if(isNetworkAvailable()){
startService(new Intent(RouteList.this,UpdaterService.class));
routeListAdapter.notifyDataSetChanged();
} else {//else of if NetworkAvailible
Toast.makeText(RouteList.this, "No Internet Connection", Toast.LENGTH_SHORT).show();
Log.d("onCreate"," setupList start");
}//end of if NetworkAvailible
}
//dialog.dismiss();
//if(dialog.isShowing()){
// dialog.dismiss();
// }
}//end of setupList
private InputStream OpenHttpConnection(String urlString) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if(!(conn instanceof HttpURLConnection)) throw new IOException("Not an HTTP connection");
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if(response == HttpURLConnection.HTTP_OK){
in = httpConn.getInputStream();
}
}
catch (Exception ex){
Log.d("Networking",ex.getLocalizedMessage());
throw new IOException("Error connection");
}
return in;
}
//http://stackoverflow.com/questions/4238921/android-detect-whether-there-is-an-internet-connection-available
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}//END OF isNetworkAvailable
}//end of