2016-03-23 8 views
0

Ich versuche Bild zu kopieren unter Code:Nicht in der Lage Bild zu kopieren ACTION_GET_CONTENT mit

Intent intentImage = new Intent(); 
     intentImage.setType("image/*"); 
     intentImage.setAction(Intent.ACTION_GET_CONTENT); 
     intent.addCategory(Intent.CATEGORY_OPENABLE); 
     startActivityForResult(intentImage, 10); 

Damit ich bin der Lage, alle Bildinhalt zu öffnen.

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 


     if (requestCode == 10) { 
      if (resultCode != RESULT_OK) return; 
      Uri selectedImageUri = data.getData(); 
      try { 

       String selectedImagePath1 = getPath(selectedImageUri); 

       File file = new File(selectedImagePath1); 
       String fna = file.getName(); 
       String pna = file.getParent(); 
       File fileImage = new File(pna, fna); 

       copyFileImage(fileImage, data.getData()); 

      } catch (Exception e) { 

      } 

     } 
    } 


private void copyFileImage(File src, Uri destUri) { 
     BufferedInputStream bis = null; 
     BufferedOutputStream bos = null; 

     try { 
      bis = new BufferedInputStream(new FileInputStream(src)); 
      bos = new BufferedOutputStream(getContentResolver().openOutputStream(destUri)); 
      byte[] buf = new byte[1024]; 
      bis.read(buf); 
      do { 
       bos.write(buf); 
      } while (bis.read(buf) != -1); 
     } catch (NullPointerException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       if (bis != null) bis.close(); 
       if (bos != null) bos.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

Jetzt bin ich erfolgreich Pfad und Name des Bildes erhalten.

Jetzt, wenn ich den obigen Code ausführen dann gibt es mir einen Fehler von erfordert android.permission.MANAGE_DOCUMENTS, or grantUriPermission().

, so habe ich die Erlaubnis, in manifest setzen:

Ich habe auch die Erlaubnis für Lese definiert und internen/externen Speicher schreiben.

Aber immer noch bekomme ich diesen Fehler.

Wie kann ich das Bild kopieren?

Antwort

0
Select picture using below code 

Intent intent = new Intent(); 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      startActivityForResult(Intent.createChooser(intent, 
        "Select Picture"), 1); 


this will open gallery, after selecting pic you will get selected pic uri in below code 

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
    switch(requestCode) { 

    case 1: 
     if(resultCode == RESULT_OK) 
     { 
      Uri selectedImageUri = data.getData(); 
      String selectedImagePath = getPath(selectedImageUri); 
      File sel = new File(selectedImagePath); 
      Bitmap bitmap = BitmapFactory.decodeFile(sel.getAbsolutePath()); 
      imageView1.setImageBitmap(bitmap); 


      Bitmap resized = Bitmap.createScaledBitmap(bitmap, 600,370, true); 

       ByteArrayOutputStream blob = new ByteArrayOutputStream(); 
       resized.compress(Bitmap.CompressFormat.JPEG, 100, blob); 
       String StrBase64 = Base64.encodeToString(blob.toByteArray(), Base64.DEFAULT); 

       imageView1.setImageBitmap(resized); 

      // Toast.makeText(getApplicationContext(), ""+selectedImagePath, Toast.LENGTH_LONG).show(); 
     } 
     break; 
    } 
    } 

     public String getPath(Uri uri) { 
      // just some safety built in 
      if(uri == null) { 
       // TODO perform some logging or show user feedback 
       return null; 
      } 
      // try to retrieve the image from the media store first 
      // this will only work for images selected from gallery 
      String[] projection = { MediaStore.Images.Media.DATA }; 
      Cursor cursor = managedQuery(uri, projection, null, null, null); 
      if(cursor != null){ 
       int column_index = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
       cursor.moveToFirst(); 
       return cursor.getString(column_index); 
      } 
      // this is our fallback here 
      return uri.getPath(); 
    } 


add permission in manifest 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

this way you will get selected image in Base64 to string 
+0

Daraus bin ich nur in der Lage zu Base64 erhalten zu konvertieren, aber ich mag auch das Bild auf SD-Karte kopieren. – deepak

0
Try this code- 
Image will copy in SaveImage folder in sd card 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
     { 
     switch(requestCode) { 

     case 1: 
      if(resultCode == RESULT_OK) 
      { 
       Uri selectedImageUri = data.getData(); 
       String selectedImagePath = getPath(selectedImageUri); 
       File sel = new File(selectedImagePath); 
       Bitmap bitmap = BitmapFactory.decodeFile(sel.getAbsolutePath()); 
       imageView1.setImageBitmap(bitmap); 


       SaveImage(bitmap); 
      } 
      break; 
     } 
     } 

     private void SaveImage(Bitmap finalBitmap) { 

      String root = Environment.getExternalStorageDirectory().toString(); 
      File myDir = new File(root + "/SaveImage");  
      myDir.mkdirs(); 
      Random generator = new Random(); 
      int n = 10000; 
      n = generator.nextInt(n); 
      String fname = "Image-"+ n +".jpg"; 
      File file = new File (myDir, fname); 
      if (file.exists()) file.delete(); 
      try { 
       FileOutputStream out = new FileOutputStream(file); 
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 
       out.flush(); 
       out.close(); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     }