1

I am new to android and want to make an app which can upload any kind of file(image, pdf, doc etc.) from my phone. Also, I want to add camera api to store images to my phone gallery. Can anybody assist me in doing this?

Below is the code for uploading the image from camera and gallery but i am getting an issue while capturing the image. the image is not saving in gallery.

public class MainActivity extends ActionBarActivity implements View.OnClickListener {
Button b1;
    ImageView i1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1=(Button)findViewById(R.id.button);
        i1=(ImageView)findViewById(R.id.imageView);
        b1.setOnClickListener(this);
    }
    private void selectImage() {

        final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Add Photo!");
        builder.setItems(options, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (options[item].equals("Take Photo"))
                {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                    startActivityForResult(intent, 1);
                }
                else if (options[item].equals("Choose from Gallery"))
                {
                    Intent intent = new   Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(intent, 2);

                }
                else if (options[item].equals("Cancel")) {
                    dialog.dismiss();
                }
            }
        });
        builder.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                File f = new File(Environment.getExternalStorageDirectory().toString());
                for (File temp : f.listFiles()) {
                    if (temp.getName().equals("temp.jpg")) {
                        f = temp;
                        break;
                    }
                }
                try {
                    Bitmap bitmap;
                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

                    bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);

                    i1.setImageBitmap(bitmap);

                    String path = android.os.Environment
                            .getExternalStorageDirectory()
                            + File.separator
                            + "Phoenix" + File.separator + "default";
                    f.delete();
                    OutputStream outFile = null;
                    File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");

                    try {
                        outFile = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                        outFile.flush();
                        outFile.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (requestCode == 2) {

                Uri selectedImage = data.getData();
                String[] filePath = { MediaStore.Images.Media.DATA };
                Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null);
                c.moveToFirst();
                int columnIndex = c.getColumnIndex(filePath[0]);
                String picturePath = c.getString(columnIndex);
                c.close();
                Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
                Log.w("path of image from gallery......******************.........", picturePath + "");
               i1.setImageBitmap(thumbnail);
            }
        }
    }

    @Override
    public void onClick(View v) {
if(v==b1)
{
    selectImage();
}
    }
}

The code snippet showing error is:

 outFile = new FileOutputStream(file);

I am getting the following error :

01-30 11:56:14.206  15924-15924/com.example.innovative.imageupload W/System.err﹕ java.io.FileNotFoundException: /storage/emulated/0/Phoenix/default/1422599174089.jpg: open failed: ENOENT (No such file or directory)
01-30 11:56:14.276  15924-15924/com.example.innovative.imageupload W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:416)
01-30 11:56:14.276  15924-15924/com.example.innovative.imageupload W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
01-30 11:56:14.276  15924-15924/com.example.innovative.imageupload W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
01-30 11:56:14.276  15924-15924/com.example.innovative.imageupload W/System.err﹕ at com.example.innovative.imageupload.MainActivity.onActivityResult(MainActivity.java:94)
01-30 11:56:14.276  15924-15924/com.example.innovative.imageupload W/System.err﹕ at android.app.Activity.dispatchActivityResult(Activity.java:5472)
01-30 11:56:14.276  15924-15924/com.example.innovative.imageupload W/System.err﹕ at android.app.ActivityThread.deliverResults(ActivityThread.java:3406)
01-30 11:56:14.276  15924-15924/com.example.innovative.imageupload W/System.err﹕ at android.app.ActivityThread.handleSendResult(ActivityThread.java:3453)
01-30 11:56:14.276  15924-15924/com.example.innovative.imageupload W/System.err﹕ at android.app.ActivityThread.access$1200(ActivityThread.java:150)
01-30 11:56:14.276  15924-15924/com.example.innovative.imageupload W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
01-30 11:56:14.276  15924-15924/com.example.innovative.imageupload W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:99)
01-30 11:56:14.276  15924-15924/com.example.innovative.imageupload W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
01-30 11:56:14.276  15924-15924/com.example.innovative.imageupload W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5283)
01-30 11:56:14.276  15924-15924/com.example.innovative.imageupload W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
01-30 11:56:14.276  15924-15924/com.example.innovative.imageupload W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)
01-30 11:56:14.276  15924-15924/com.example.innovative.imageupload W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
01-30 11:56:14.276  15924-15924/com.example.innovative.imageupload W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
01-30 11:56:14.276  15924-15924/com.example.innovative.imageupload W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
01-30 11:56:14.276  15924-15924/com.example.innovative.imageupload W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
01-30 11:56:14.286  15924-15924/com.example.innovative.imageupload W/System.err﹕ at libcore.io.Posix.open(Native Method)
01-30 11:56:14.286  15924-15924/com.example.innovative.imageupload W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
01-30 11:56:14.286  15924-15924/com.example.innovative.imageupload W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:400)
01-30 11:56:14.286  15924-15924/com.example.innovative.imageupload W/System.err﹕ ... 16 more

I am not able to find the error why the image is not saving into the gallery. Please assist me in doing this. Thanks in Advance.

Param
  • 268
  • 1
  • 3
  • 12
  • Show your work buddy! This site is intended to help out and guide people if they are wrong. Be specific, add codes, ask if you don't understand a section. – Kailas Jan 29 '15 at 08:02
  • @Kailas - now tell me what can i do ? – Param Jan 29 '15 at 09:29
  • you can do this with web service. create your web-service in php or Asp.net which will Upload images, docx etc to server . – Hemz Jan 29 '15 at 10:48
  • @Hemz- But tell me what is the problem with my code? please. – Param Jan 30 '15 at 06:38

1 Answers1

0

if You use sdk>=23, nd not using external storage, then that will show the problem, I have the same problem, you sure that you uses the permissions in that activity // To be safe, you should check that the SDCard is mounted // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "SARApp");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
            if (pictureFile == null){
                Log.d("SARCamera", "failed to create directory");

            }

            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {
                Log.d(TAG, "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d(TAG, "Error accessing file: " + e.getMessage());
            }
        }
    }
Apurba
  • 3
  • 4