0

나는 안드로이드를 처음 접하니 나의 휴대폰에서 어떤 종류의 파일 (이미지, pdf, 의사 등)을 올릴 수있는 앱을 만들고 싶습니다. 또한, 내 전화 갤러리에 이미지를 저장할 카메라 API를 추가하고 싶습니다. 아무도이 일을 도와 줄 수 있습니까?이미지가 캡처되지 않고 android에 저장되지 않음

아래는 카메라 및 갤러리에서 이미지를 업로드하는 코드이지만 이미지를 캡처하는 동안 문제가 발생합니다. 이미지가 갤러리에 저장되지 않습니다.

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(); 
} 
    } 
} 

오류를 보여주는 코드이다 : 나는 다음과 같은 오류가 무엇입니까

outFile = new FileOutputStream(file); 

:

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 

을 나는 이미지로 저장되지 않는 이유 오류를 찾을 수 없습니다입니다 갤러리. 이 일을 도와주세요. 미리 감사드립니다.

+0

작업 친구 표시! 이 사이트는 사람들이 잘못하면 안내하고 돕기위한 것입니다. 구체적으로 코드를 추가하고 섹션을 이해하지 못하는지 묻습니다. – Kailas

+0

@Kailas - 지금 무엇을 할 수 있는지 말해주세요. – Param

+0

웹 서비스에서이 작업을 수행 할 수 있습니다. PHP 또는 Asp.net에서 이미지, docx 등을 서버에 업로드하는 웹 서비스를 만드십시오. – Hemz

답변

0

외부 저장소를 사용하지 않고 nd가 sdk> 23을 사용하는 경우 문제가 발생합니다. 동일한 문제가 있습니다. 해당 작업의 사용 권한을 사용했는지 확인하십시오. // 안전해야합니다. 이 작업을 수행하기 전에 SDCard가 // Environment.getExternalStorageState()를 사용하여 마운트되었는지 확인하십시오.

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()); 
      } 
     } 
    } 
관련 문제