2016-04-13 11 views
1

私はsdcardから写真を撮ろうとしていますが、ユーザーがGoogleフォトから選択してもギャラリーのようなアプリから選択するとcurserindexoutofboundエラーまたはファイルマネージャ。ここにコードがあります。Googleフォトから写真をアップロードしますが、ギャラリーやファイルマネージャーから写真をアップロードしないでください

OnActivity結果 ' {IF(requestCode == PICK_IMAGE_REQUEST & &のresultCode == Activity.RESULT_OK & &ヌルデータ= & & data.getData()= NULL!) {ウリURI = NULL; 文字列realPath = null;

 try 
     { 
      uri = data.getData(); 
      Log.e("uri",uri.toString()); 
      Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri); 

      imageView.setImageBitmap(bitmap); 

      String imagePath = getRealPathFromURI(uri); 

      File source = new File(imagePath); 

      String destinationPath = Environment.getExternalStorageDirectory().toString() + "/IASFolders/"+Configuration.empcode+".jpg"; 



      File destination = new File(destinationPath); 
      try 
      { 
       InputStream in = new FileInputStream(source.getAbsolutePath()); 
       OutputStream out = new FileOutputStream(destination.getAbsolutePath()); 

       byte[] buf = new byte[1024]; 
       int len; 
       while ((len = in.read(buf)) > 0) 
       { 
        out.write(buf, 0, len); 
       } 

       in.close(); 
       out.close(); 

      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
       Log.d("File Copy Exception", e.toString()); 
      } 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 

}

`{getRealPathFromURI機能

private String getRealPathFromURI(Uri contentURI) { 
    String result; 
    Cursor cursor = getActivity().getContentResolver() 
      .query(contentURI,new String[]{MediaStore.Images.Media.DATA},MediaStore.Images.Media.DISPLAY_NAME+"=?" ,new String[]{Configuration.empcode},null); 

    /* 
    * Cursor imageCursor=getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,new String[]{MediaStore.Images.Media.DATA},MediaStore.Images.Media.DISPLAY_NAME+"=?" ,new String[]{imageTitle},null); 
        imageCursor.moveToFirst(); 
        String imageData=imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA)); 
        Long imageSize=imageCursor.getLong(imageCursor.getColumnIndex(ImageColumns.SIZE)); 
        Toast.makeText(getApplicationContext(), String.valueOf(imageSize), Toast.LENGTH_LONG).show();*/ 
    if (cursor == null) 
    { 
     result = contentURI.getPath(); 
    } 
    else 
    { 
     cursor.moveToFirst(); 
     int idx = cursor.getColumnIndex(MediaStore.Images.Media.DATA); 
     result = cursor.getString(idx); 
     cursor.close(); 
    } 
    return result; 
} 

}`

答えて

1

この

//while setting Intent 
    if (Build.VERSION.SDK_INT <= 19) { 
              Intent intent = new Intent(); 
              intent.setType("image/*"); 
              intent.setAction(Intent.ACTION_GET_CONTENT); 
              intent.addCategory(Intent.CATEGORY_OPENABLE); 
              startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE); 
             } else if (Build.VERSION.SDK_INT > 19) { 
              Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
              startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE); 


       } 


       // get result after selecting image from Gallery 
       @Override 
       public void onActivityResult(int requestCode, int resultCode, Intent data) { 
        super.onActivityResult(requestCode, resultCode, data); 

        if (requestCode == PICK_IMAGE && resultCode == getActivity().RESULT_OK && null != data) { 
         Uri selectedImageUri = data.getData(); 
         String selectedImagePath = getRealPathFromURIForGallery(selectedImageUri); 
         decodeFile(selectedImagePath); 
        } 
       } 

       public String getRealPathFromURIForGallery(Uri uri) { 
        if (uri == null) { 
         return null; 
        } 
        String[] projection = {MediaStore.Images.Media.DATA}; 
        Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null, null); 
        if (cursor != null) { 
         int column_index = cursor 
           .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
         cursor.moveToFirst(); 
         return cursor.getString(column_index); 
        } 
        return uri.getPath(); 
       }  
        // decode image 
       public void decodeFile(String filePath) { 
        // Decode image size 
        BitmapFactory.Options o = new BitmapFactory.Options(); 
        o.inJustDecodeBounds = true; 
        BitmapFactory.decodeFile(filePath, o); 
        // The new size we want to scale to 
        final int REQUIRED_SIZE = 1024; 
        // Find the correct scale value. It should be the power of 2. 
        int width_tmp = o.outWidth, height_tmp = o.outHeight; 
        int scale = 1; 
        while (true) { 
         if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) 
          break; 
         width_tmp /= 2; 
         height_tmp /= 2; 
         scale *= 2; 
        } 

        // Decode with inSampleSize 
        BitmapFactory.Options o2 = new BitmapFactory.Options(); 
        o2.inSampleSize = scale; 
        bitmap = BitmapFactory.decodeFile(filePath, o2); 
        Security connection = new Security(context); 
        Boolean isInternetPresent = connection.isConnectingToInternet(); // true or false 
        if (isInternetPresent) { 
         // submit usr information to server 
         //first upload file 
         updateUserProfileImage(); 
         Log.i("IMAGEPATH", "" + imagePath); 
        } 
        profileImageView.setImageBitmap(bitmap); 
       } 
+0

のようにこのコードを試してみてください取り組んでいますが、私のコードを見れば私はされますイメージを取得して別のイメージにコピーしようとするフォルダには例外があります。ありがとうございます。 –

+0

@MithileshIzardar http://stackoverflow.com/questions/8664440/how-to-copy-image-file-from-gallery-to-another-folder-programatically-in-android ..選択された画像パス –

+0

@MilesilesIzardarまたはこのhttp://stackoverflow.com/questions/4921183/android-copy-image-from-gallery-folder-onto-sd-card-alternative-folderのさらなるコピーについては、これを参考にしてください –

関連する問題