IllegalArgumentException on MediaMetadataRetriever.setDataSource() - java

I am trying to get few frames from a video file. And getting java.lang.IllegalArgumentException in the setDataSource line below.
String root = Environment.getExternalStorageDirectory().getAbsolutePath()+"/";
String path=root + "master" + File.separator +"master.mp4";
Uri videoFileUri=Uri.parse(path);
File myFile = new File(path);
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(myFile.getAbsolutePath()); //getting error here
ArrayList<Bitmap> rev=new ArrayList<Bitmap>();
//Create a new Media Player
MediaPlayer mp = MediaPlayer.create(getBaseContext(), videoFileUri);
int millis = mp.getDuration();
for(int i=0;i<millis;i+=100){
Bitmap bitmap=retriever.getFrameAtTime(i,OPTION_CLOSEST_SYNC);
rev.add(bitmap);}
Error Log
E/AndroidRuntime: FATAL EXCEPTION: main
Process: in.mm.video, PID: 11576
java.lang.RuntimeException: Unable to start activity ComponentInfo{in.mm.video/in.mm.video.RecognizeActivity}: java.lang.IllegalArgumentException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.IllegalArgumentException
at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:71)
at in.mm.video.RecognizeActivity.onCreate(RecognizeActivity.java:178)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
Application terminated.

Related

Save multiple images from drawable folder with one button click

I want to download multiple images from my app drawable with one button click and save it in device internal/external storage of my Android app.
How do I achieve this?
UPDATE
I tried it, but I'm having one issue.
downloadTest.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick (View v){
//path here is for internal storage (Option 2), replace it with the other line above from Option 1 to save images to SD card
String path = new File(getFilesDir(), "/Space Wallpapers").getAbsolutePath();
//ids are drawables resource ids
int[] ids = new int[]{ R.drawable.abduction,
R.drawable.green_nightstreak,
R.drawable.hero,
R.drawable.ic_night,
R.drawable.light_streak,
R.drawable.moon_with_stars,
R.drawable.orange_nightstreak,};
int i = 1;
for (int res : ids) {
//Creates the bitmap object from the given resource
Bitmap b = BitmapFactory.decodeResource(getResources(), res);
File file = new File(path, "image" + i + ".PNG");
//This creates the file and it's parent if doesn't exist
file.getParentFile().mkdirs();
try {
FileOutputStream fileOut = new FileOutputStream(file);
b.compress(Bitmap.CompressFormat.PNG, 100, fileOut);
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
i++;
}
};
});
The issue is that it's not the images are not downloading.
**This is what my "Run" log is showing **
D/ViewRootImpl#372fd66[TestActivity]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl#372fd66[TestActivity]: ViewPostImeInputStage processPointer 1
W/System.err: java.io.FileNotFoundException: /data/user/0/com.khumomashapa.notes.debug/files/Space Wallpapers/image1.PNG (Is a directory)
at java.io.FileOutputStream.open(Native Method)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
at com.khumomashapa.notes.activities.TestActivity$1.onClick(TestActivity.java:54)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
W/System.err: at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1073)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
W/System.err: java.io.FileNotFoundException: /data/user/0/com.khumomashapa.notes.debug/files/Space Wallpapers/image2.PNG (Is a directory)
at java.io.FileOutputStream.open(Native Method)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
at com.khumomashapa.notes.activities.TestActivity$1.onClick(TestActivity.java:54)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1073)
W/System.err: at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
W/System.err: java.io.FileNotFoundException: /data/user/0/com.khumomashapa.notes.debug/files/Space Wallpapers/image3.PNG (Is a directory)
W/System.err: at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
at com.khumomashapa.notes.activities.TestActivity$1.onClick(TestActivity.java:54)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1073)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
W/System.err: java.io.FileNotFoundException: /data/user/0/com.khumomashapa.notes.debug/files/Space Wallpapers/image4.PNG (Is a directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
at com.khumomashapa.notes.activities.TestActivity$1.onClick(TestActivity.java:54)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1073)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
W/System.err: java.io.FileNotFoundException: /data/user/0/com.khumomashapa.notes.debug/files/Space Wallpapers/image5.PNG (Is a directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
at com.khumomashapa.notes.activities.TestActivity$1.onClick(TestActivity.java:54)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1073)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
W/System.err: java.io.FileNotFoundException: /data/user/0/com.khumomashapa.notes.debug/files/Space Wallpapers/image6.PNG (Is a directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
at com.khumomashapa.notes.activities.TestActivity$1.onClick(TestActivity.java:54)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1073)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
W/System.err: java.io.FileNotFoundException: /data/user/0/com.khumomashapa.notes.debug/files/Space Wallpapers/image7.PNG (Is a directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
W/System.err: at com.khumomashapa.notes.activities.TestActivity$1.onClick(TestActivity.java:54)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1073)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:154)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6682)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
I/Choreographer: Skipped 134 frames! The application may be doing too much work on its main thread.
I assume that by "download" you mean save/export. Then, this code should be working fine.
Option 1:
If you want to save images to external storage (SD card) add this line above the code:
String path = new File(Environment.getExternalStorageDirectory(), "Images").getAbsolutePath();
Add: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> to manifest
Option 2
If you want to save images to internal storage, just add:
String path = new File(getFilesDir(), "Images").getAbsolutePath() ;
Then:
yourButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick (View v){
//path here is for internal storage (Option 2), replace it with the other line above from Option 1 to save images to SD card
String path = new File(getFilesDir(), "Images").getAbsolutePath();
//ids are drawables resource ids
int[] ids = new int[]{R.drawable.imageId1, R.drawable.imageId2, R.drawable.imageId3};
int i = 1;
for (int res : ids) {
//Creates the bitmap object from the given resource
Bitmap b = BitmapFactory.decodeResource(getResources(), res);
File file = new File(path, "image" + i + ".PNG");
//This creates the file and it's parent if doesn't exist
file.getParentFile().mkdirs();
try {
FileOutputStream fileOut = new FileOutputStream(file);
b.compress(Bitmap.CompressFormat.PNG, 100, fileOut);
fileOut.close;
} catch (Exception e) {
e.printStackTrace();
}
i++;
}
});
Output:
R.drawable.imageId1 will be saved to path/image1.PNG
R.drawable.imageId2 will be saved to path/image2.PNG
R.drawable.imageId3 will be saved to path/image3.PNG

My app is crashing when I call my function by pressing a button

I am trying to make a simple gallery app. When I try this code:
public void previousTap(View view){
ImageView img = (ImageView) view;
if (img.getTag() == "img_one") {
img.setImageResource(R.drawable.img_five);
img.setTag("img_five");
} else if (img.getTag() == "img_two") {
img.setImageResource(R.drawable.img_one);
img.setTag("img_one");
} else if (img.getTag() == "img_three") {
img.setImageResource(R.drawable.img_two);
img.setTag("img_two");
} else if (img.getTag() == "img_four") {
img.setImageResource(R.drawable.img_three);
img.setTag("img_three");
} else if (img.getTag() == "img_five") {
img.setImageResource(R.drawable.img_four);
img.setTag("img_four");
}
}
When I run my project and install the app and click on the previous button, my app crashes. Why? Also, is there any way to get the tag of an imageview in activity_main.xml rather than typing "ImageView img = (ImageView) view;"(Only if it is wrong)?
The error I get:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.wali.photoframegallery, PID: 6166
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
at android.view.View.performClick(View.java:6603)
at android.view.View.performClickInternal(View.java:6576)
at android.view.View.access$3100(View.java:780)
at android.view.View$PerformClick.run(View.java:26090)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:235)
at android.app.ActivityThread.main(ActivityThread.java:6760)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
at android.view.View.performClick(View.java:6603) 
at android.view.View.performClickInternal(View.java:6576) 
at android.view.View.access$3100(View.java:780) 
at android.view.View$PerformClick.run(View.java:26090) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:235) 
at android.app.ActivityThread.main(ActivityThread.java:6760) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911) 
Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatButton cannot be cast to android.widget.ImageView
at com.wali.photoframegallery.MainActivity.nextTap(MainActivity.java:32)
at java.lang.reflect.Method.invoke(Native Method) 
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397) 
at android.view.View.performClick(View.java:6603) 
at android.view.View.performClickInternal(View.java:6576) 
at android.view.View.access$3100(View.java:780) 
at android.view.View$PerformClick.run(View.java:26090) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:235) 
at android.app.ActivityThread.main(ActivityThread.java:6760) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)
EDIT:
I tried changing this:
ImageView img = (ImageView) view;
To this:
View img = (View) view;
It gave me no errors, but there is no more functionality. What's wrong now?
You can see your problem in next line: Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatButton cannot be cast to android.widget.ImageView
of your stack trace. You can try to cast view in AppCompatButton, or change AppCompatButton to ImageView in your layout

Cannot retrieve the data when trying to login the system (Android Studio)

I'm begginer in Android programming, I'm trying to make the login function. This system will be accessed by two users which are admin and users. The admin just need to enter the "admin" for the username and password to access the system. However, the users needs to register their account before login the system.
I'm trying to find the errors but still don't know the error occurrence. Please help me to solve the errors. Thank You.
DatabaseOperations.java
public Cursor getInformation(DatabaseOperations dob)
{
SQLiteDatabase database=dob.getReadableDatabase();
String[] columns={TableInfo.USERNAME,TableInfo.USERPASS};
Cursor cr=database.query(TableInfo.TABLE_NAME,columns,null,null,null,null,null);
return cr;
}
MainActivity.java
public void onClick(View view)
Toast.makeText(getBaseContext(), "Please wait....", Toast.LENGTH_LONG).show();
txtusername = Username.getText().toString();
txtpassword = Password.getText().toString();
DatabaseOperations dop = new DatabaseOperations(this, null, null, 1);
Cursor CR = dop.getInformation(dop);
CR.moveToFirst();
boolean login_status = false;
String NAME = "";
if (txtusername.equals("Admin") && (txtpassword).equals("Admin")) {
Intent in = new Intent(MainActivity.this, AdminMenu.class);
startActivity(in);
} else {
do {
if (txtusername.equals(CR.getString(0)) && (txtpassword.equals(CR.getString(1)))) {
login_status = true;
NAME = CR.getString(0);
}
} while (CR.moveToNext());
if (login_status) {
Toast.makeText(getBaseContext(), "Login Success....\n Welcome " + NAME, Toast.LENGTH_LONG).show();
Intent i=new Intent(MainActivity.this,UserMenu.class);
Bundle bundle=new Bundle();
bundle.putString("username",NAME);
i.putExtras(bundle);
startActivity(i);
finish();
} else {
Toast.makeText(getBaseContext(), "Login Failed...", Toast.LENGTH_LONG).show();
Intent i = new Intent();
Bundle bundle=new Bundle();
bundle.putString("username",NAME);
i.putExtras(bundle);
startActivity(i);
finish();
}
}
}
This is the error...
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.google.mobile_flexi_parking, PID: 6777
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { (has extras) }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1798)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
at android.app.Activity.startActivityForResult(Activity.java:3917)
at android.app.Activity.startActivityForResult(Activity.java:3877)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:842)
at android.app.Activity.startActivity(Activity.java:4200)
at android.app.Activity.startActivity(Activity.java:4168)
at com.google.mobile_flexi_parking.MainActivity.onclickLogin(MainActivity.java:67)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
In your else part you have blank intent which doesn't point any activity
Intent i = new Intent();
I think this is causing error.

android Unable to start activity ComponentInfo NULL Point Exception

My app work strange on diffrent phones - once everything is fine and other time it crash when i try to make photo and save it in my phone's memory. Photo shows up in folder in memory, but when we should come back to application it is shut down:
06-14 23:58:37.448 12344-12344/com.apps.madzia.photo_application E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.apps.madzia.photo_application, PID: 12344
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apps.madzia.photo_application/com.apps.madzia.photo_application.activity.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2412)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4053)
at android.app.ActivityThread.access$1000(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
at android.widget.GridView.setAdapter(GridView.java:187)
at com.apps.madzia.photo_application.fragments.FragmentGallery.onCreateView(FragmentGallery.java:42)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1917)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:544)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1177)
at android.app.Activity.performStart(Activity.java:5468)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2385)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4053)
            at android.app.ActivityThread.access$1000(ActivityThread.java:174)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5593)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
The code of my function to make photos:
public void make_photo() {
Calendar c;
c = Calendar.getInstance();
imageName = "fotka_" + c.get(Calendar.YEAR) + "_" + c.get(Calendar.MONTH) + "_" + c.get(Calendar.DAY_OF_MONTH)
+ "_" + c.get(Calendar.HOUR_OF_DAY) + "_" + c.get(Calendar.MINUTE) + "_" + c.get(Calendar.SECOND);
File katalog = new File(IMAGE_FILE_PATH);
File photo = new File(IMAGE_FILE_PATH+imageName + ".jpg");
if( Environment.getExternalStorageState() == Environment.MEDIA_UNKNOWN || Environment.getExternalStorageState() == Environment.MEDIA_REMOVED || Environment.getExternalStorageState() == Environment.MEDIA_UNMOUNTED) {
Log.d("ExternalStorage", "cannot find memory SD card");
File file = new File(getFilesDir(), imageName);
IMAGE_FILE_PATH = file.getParent()+"/";
Log.d("IMAGE_FILE_PATH", IMAGE_FILE_PATH);
}else {
}
Log.d("photo", photo.getPath());
if (!katalog.exists()) {
katalog.mkdirs();
}
try {
// fotka = File.createNewFile(imageName, ".jpg", katalog);
photo.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Intent zdjecie = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
zdjecie.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
startActivityForResult(zdjecie, 1);
}
Caused by: java.lang.NullPointerException
at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
at android.widget.GridView.setAdapter(GridView.java:187)
at com.apps.madzia.photo_application.fragments.FragmentGallery.onCreateView(FragmentGallery.java:42)
This means that in your FragmentGallery in onCreateView method on line 42 u are setting null ArrayAdapter

BufferedRead runtime crash

My app crash when encounters BufferedReader operation. Ive tried to implement the code inside an asyncTask classe, because ive read that the crash is caused by the ui thread, but nothing has changed.
public class URLConnectionReader extends AsyncTask<URL, Void, String> {
String result = null;
#Override
protected String doInBackground(URL... params) {
String result = null;
URL url = (params[0]);
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(
url.openStream()
)
);
}catch (IOException e){
e.printStackTrace();
}
return result;
}
}
Logs
03-02 13:28:43.215 25186-25186/com.example.jsonreader E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.jsonreader, PID: 25186
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jsonreader/com.example.jsonreader.MainActivity}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:323)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:190)
at java.net.URL.openStream(URL.java:470)
at com.example.jsonreader.URLConnectionReader.doInBackground(URLConnectionReader.java:38)
at com.example.jsonreader.MainActivity.onCreate(MainActivity.java:27)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
MainActivity code
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
URL address = new URL("http://www.youth-stories.com/api/all.php");
URLConnectionReader reader = new URLConnectionReader();
String result = reader.doInBackground(address);
Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
}catch (IOException e){
e.printStackTrace();
}
i solved the problem to get back data using the .get instruction
String result = reader.execute(address).get();

Categories