1

I could save a picture in the folder I want by my code but that wasn't good enough, because my code was giving a copy of image in my folder and another one in the gallery (DCIM).

The code:

       Btngetdata.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, 2);
        }
    });
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        File a = new File (Environment.getExternalStorageDirectory().getAbsolutePath() + "/splash/"+name+"/");
        if (!a.exists()) {
            a.mkdirs();
        }
        if (requestCode == 2) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            ImageView test = (ImageView) findViewById(R.id.test);
            test.setImageBitmap(photo);
            try{
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
                String currentDateandTime = sdf.format(new Date()).replace(" ","");
                FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/splash/"+name+"/"+currentDateandTime+".jpg");
                photo.compress(Bitmap.CompressFormat.PNG, 100 , out);

                out.flush();
                out.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

I can't use the code below or anything like this.

       Btngetdata.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            f = new File(Environment.getExternalStorageDirectory().getPath() + "/tmp/"+"tmp.png");
            if (!f.exists()) {
                f.mkdirs();
            }
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
            startActivityForResult(cameraIntent, 2);
        }
    });

them all give me below Logcat

04-17 20:41:24.974: E/AndroidRuntime(13913): FATAL EXCEPTION: main
04-17 20:41:24.974: E/AndroidRuntime(13913): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {com.example.sofrehkhane/com.sofrehKhane.slidingmenu.slidinglist.SingleItemView}: java.lang.NullPointerException
04-17 20:41:24.974: E/AndroidRuntime(13913):    at     android.app.ActivityThread.deliverResults(ActivityThread.java:3462)
04-17 20:41:24.974: E/AndroidRuntime(13913):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3505)
04-17 20:41:24.974: E/AndroidRuntime(13913):    at android.app.ActivityThread.access$1100(ActivityThread.java:150)
04-17 20:41:24.974: E/AndroidRuntime(13913):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1346)
04-17 20:41:24.974: E/AndroidRuntime(13913):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 20:41:24.974: E/AndroidRuntime(13913):    at android.os.Looper.loop(Looper.java:213)
04-17 20:41:24.974: E/AndroidRuntime(13913):    at android.app.ActivityThread.main(ActivityThread.java:5225)
04-17 20:41:24.974: E/AndroidRuntime(13913):    at java.lang.reflect.Method.invokeNative(Native Method)
04-17 20:41:24.974: E/AndroidRuntime(13913):    at java.lang.reflect.Method.invoke(Method.java:525)
04-17 20:41:24.974: E/AndroidRuntime(13913):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
04-17 20:41:24.974: E/AndroidRuntime(13913):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
04-17 20:41:24.974: E/AndroidRuntime(13913):    at dalvik.system.NativeStart.main(Native Method)
04-17 20:41:24.974: E/AndroidRuntime(13913): Caused by: java.lang.NullPointerException
04-17 20:41:24.974: E/AndroidRuntime(13913):    at com.sofrehKhane.slidingmenu.slidinglist.SingleItemView.onActivityResult(SingleItemView.java:102)
04-17 20:41:24.974: E/AndroidRuntime(13913):    at android.app.Activity.dispatchActivityResult(Activity.java:5322)
04-17 20:41:24.974: E/AndroidRuntime(13913):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3458)
04-17 20:41:24.974: E/AndroidRuntime(13913):    ... 11 more

I'll be glad if you guide me to solve the problem.

I want to use "setOnClickListener(new OnClickListener(){" for a button to take pic and use "public void onActivityResult(int requestCode, int resultCode, Intent data) {" to save taken pic.

UPDATE: By using this link I got the best answer and performance for my app.

mins
  • 6,478
  • 12
  • 56
  • 75
alireza
  • 518
  • 5
  • 15

1 Answers1

0

In the line

Bitmap photo = (Bitmap) data.getExtras().get("data");

data is null according to the stacktrace. You have to check for null there and act accordingly.

Additionally, check this answer: https://stackoverflow.com/a/10779050/2367849

Community
  • 1
  • 1
cygery
  • 2,309
  • 3
  • 18
  • 25
  • I updated the question with the code which works but is not fine. it gives me a copy of the picture in the folder I want and another copy in the gallery. – alireza Apr 17 '15 at 17:21
  • As far as I can tell you can't prevent the default camera app from saving the image to the gallery. You might be able to delete it from the gallery, though. – cygery Apr 17 '15 at 17:25
  • I would do that, I have tested a solution that I found on stackoverflow but it doesn't solve this issue. It's been 12 hours I'm searching for a working solution but nothing show up. – alireza Apr 17 '15 at 17:37
  • I don't know what terms you used in your search but searching for "android delete image from gallery" shows a promising solution in the first result for me. – cygery Apr 17 '15 at 17:42
  • OK, I finally found a solution from the link you shared. I reached to this answer, the code deletes the image. http://stackoverflow.com/a/8166877/4620936 – alireza Apr 17 '15 at 17:54
  • The solution in the following answer might be better/more complete than the one you linked: http://stackoverflow.com/a/6600148/2367849 – cygery Apr 17 '15 at 17:58