2015-04-12 39 views
0

在我的项目中,我从图库中选择图像,并希望将其从图库移动到我的SD卡中的其他文件夹....到目前为止,我已经完成了选择图像和图像的路径也得到了......现在我要的是图像从该文件夹移动.... 由于事先将图像从一个文件夹移动到另一个android sdcard

MainActivity

public class MainActivity extends ActionBarActivity { 

    ImageView iv; 
    Button load,cp; 
    TextView tv,tvpath; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     iv = (ImageView)findViewById(R.id.imageView); 

     tv = (TextView)findViewById(R.id.textView); 
     tvpath = (TextView)findViewById(R.id.textView2); 

     load = (Button)findViewById(R.id.button); 
     cp = (Button)findViewById(R.id.button2); 

     load.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
       i.setType("image/*"); 
       startActivityForResult(i, 1); 
      } 
     }); 

    } 

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

     super.onActivityResult(requestCode, resultCode, data); 

     if(requestCode== 1 && resultCode == RESULT_OK && data != null){ 
      String realpath; 
      if (Build.VERSION.SDK_INT < 11){ 

       realpath = RealPathUtil.getRealPathFromURI_BelowAPI11(this,data.getData()); 

      }else if (Build.VERSION.SDK_INT < 19){ 

       realpath = RealPathUtil.getRealPathFromURI_BelowAPI11to18(this, data.getData()); 

      }else{ 

       realpath = RealPathUtil.getRealPathFromURI_API19(this, data.getData()); 
      } 

      settext(Build.VERSION.SDK_INT,data.getData().getPath(),realpath); 
      changepath(realpath); 



      /* Uri pickimage = data.getData(); 

      String[] filepath = {MediaStore.Images.Media.DATA}; 


      Cursor cursor = getContentResolver().query(pickimage,filepath,null,null,null); 
      cursor.moveToFirst(); 

      String imgpath = cursor.getString(cursor.getColumnIndex(filepath[0])); 

      iv.setImageBitmap(BitmapFactory.decodeFile(imgpath)); 

      cursor.close(); 

      Toast.makeText(getApplicationContext(),filepath[0],Toast.LENGTH_SHORT).show(); 
      */ 
     } 

    } 
//tried to chage the path here 

    private void changepath(String realpath) { 
     File rp = new File(realpath); 
     File cp = new File("/mnt/sdcard/myapp/"); 

     rp.renameTo(cp); 
     String s = rp.getAbsolutePath(); 


     Toast.makeText(getApplicationContext(),"new path is:"+s,Toast.LENGTH_LONG).show(); 

    } 

    private void settext(int sdkInt, String path, String realpath) { 
      this.tv.setText("uri path :"+path); 
     this.tvpath.setText("Real path :"+realpath); 

     Uri uri = Uri.fromFile(new File(realpath)); 

     iv.setImageURI(uri); 
    } 

    } 
**RealPathUtil** 

    public class RealPathUtil { 
    public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) { 
     String[] proj = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null); 
     int column_index 
       = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    } 


    public static String getRealPathFromURI_BelowAPI11to18(Context context, Uri contentUri) { 
     String[] proj = { MediaStore.Images.Media.DATA }; 
     String result = null; 

     CursorLoader cursorLoader = new CursorLoader(
       context, 
       contentUri, proj, null, null, null); 
     Cursor cursor = cursorLoader.loadInBackground(); 

     if(cursor != null){ 
      int column_index = 
        cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      result = cursor.getString(column_index); 
     } 
     return result; 
    } 

    public static String getRealPathFromURI_API19(Context context, Uri data) { 

     String filepath = ""; 

     String wholeID = DocumentsContract.getDocumentId(data); 

     String[] column = {MediaStore.Images.Media.DATA}; 

     String sel = MediaStore.Images.Media._ID + "=?"; 

     String id = wholeID.split(":")[1]; 
     Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,column,sel,new String[]{ id },null); 

     int columnIndex = cursor.getColumnIndex(column[0]); 
     if (cursor.moveToFirst()){ 
      filepath = cursor.getString(columnIndex); 

     } 
     cursor.close(); 
     return filepath; 
    } 
} 
+0

是的。好。问题是什么?在你告诉事情出错的地方之前,我没有看你的代码。 – greenapps

回答

0

请确保您有权限写入文件。你这样做,把这个:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 

在AndroidManifest.xml中

如果你的方法移动位图失败,你可以试试这个方法:

path_source是位图的路径和path_destination是新的道路。 (您要移动位图的位置)

public static void MoveFile(String path_source, String path_destination) throws IOException { 
    File file_Source = new File(path_source); 
    File file_Destination = new File(path_destination); 

    FileChannel source = null; 
    FileChannel destination = null; 
    try { 
     source = new FileInputStream(file_Source).getChannel(); 
     destination = new FileOutputStream(file_Destination).getChannel(); 

     long count = 0; 
     long size = source.size();    
     while((count += destination.transferFrom(source, count, size-count))<size); 
     file_Source.delete(); 
    } 
    finally { 
     if(source != null) { 
      source.close(); 
     } 
     if(destination != null) { 
      destination.close(); 
     } 
    } 
} 
+1

'if(!destFile.exists()){ destFile.createNewFile(); }'。请删除。它不需要,因为新的FileOutputStream会关心它。 – greenapps

+2

即使目标文件无法创建或未完全复制,您仍在删除源文件。用户不会喜欢那样。 – greenapps

+0

好的,我改变了它,你认为它现在可行吗? – Alexanus

相关问题