I am trying to store an image from the net into my emulator's sdcard. However I get an expection more specifically a null object reference.
W/AQuery: reporting:java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.File.getAbsolutePath()' on a null object reference
at testing.theo.androidqueryexample.MainActivity.handfile(MainActivity.java:69)
at testing.theo.androidqueryexample.MainActivity.access$000(MainActivity.java:19)
at testing.theo.androidqueryexample.MainActivity$1.callback(MainActivity.java:61)
at testing.theo.androidqueryexample.MainActivity$1.callback(MainActivity.java:56)
at com.androidquery.callback.AbstractAjaxCallback.callback(AbstractAjaxCallback.java:506)
at com.androidquery.callback.AbstractAjaxCallback.afterWork(AbstractAjaxCallback.java:1299)
at com.androidquery.callback.AbstractAjaxCallback.run(AbstractAjaxCallback.java:998)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
My code is shown below.
public class MainActivity extends AppCompatActivity {
AQuery aq;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aq = new AQuery(this);
/*Button b = (Button)findViewById(R.id.button);
b.setText("new text");
b.setBackgroundColor(Color.RED);
b.setEnabled(true);*/
aq.id(R.id.button).clicked(this, "clickhandle");
}
public void clickhandle(View view){
ProgressDialog pd = new ProgressDialog(this);
pd.setTitle("Loading Image");
pd.setMessage("Please wait...");
aq.id(R.id.button).text("aquery text").backgroundColor(Color.RED).
textColor(Color.YELLOW).enabled(true);
//aq.id(R.id.imageView).progress(pd).image("http://static.panoramio.com/photos/large/127854802.jpg",true,true,0,0,null,AQuery.FADE_IN);
File f = new File(Environment.getExternalStorageDirectory(),"myfiles/myimage.jpg");
if(!f.exists())
try {
f.mkdirs();
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
aq.download("http://static.panoramio.com/photos/large/127854802.jpg", f, new AjaxCallback<File>(){
@Override
public void callback(String url, File object, AjaxStatus status) {
super.callback(url, object, status);
handfile(object);
}
});
}
private void handfile(File object) {
Toast.makeText(this,"got file: " +object.getAbsolutePath().toString(),Toast.LENGTH_LONG).show();
}
}
Line 69 corresponds to the Toast message inside the handlefile(...) method.
What could be wrong?
I declared the WRITE_EXTERNAL_STORAGE and INTERNT permissions in the manifest file.
Thanks.
The problem I found is that your creating the file myimage.jpg
inside directory myfiles
but that directory doesn't exists. Try creating the directory structure before creating the file.
File f = new File f = new File(Environment.getExternalStorageDirectory(),"myfiles/myimage.jpg");
if(!f.exists())
try {
f.getParentFile().mkdirs(); //create missing parents
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Hope this helps