如何将文件复制到新文件夹?

时间:2014-06-11 19:35:20

标签: android image file stream copy

我有一个课程,向我展示一个图像+ 2 textview。按下按钮SAVE后,它应该创建一个名为image despription的新文件夹,并将图像复制到该文件夹​​。创建文件夹没问题,但文件中没有文件。有人可以帮帮我吗?

public class TagActivity extends Activity {

private ImageMetadata imgData;
private TextView imageTitle;
private TextView imageDescription;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tag);

    final Intent i = getIntent();
    final String imgPath = i.getExtras().getString("imgPath");
    final File imgFile = new File(imgPath);
    final String imgName = imgFile.getName();
    imgData = new ImageMetadata(this, imgName);

    imageTitle = (TextView) findViewById(R.id.imageTitle);
    imageDescription = (TextView) findViewById(R.id.imageDescription);
    final ImageView imageView = (ImageView) findViewById(R.id.imageView);

    imageView.setImageBitmap(
            ScaledBitmapFactory.decodeSampledBitmapFromFile(imgPath, imageView.getWidth(), 300));

    imageView.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            startActivity(new Intent(Intent.ACTION_VIEW,
                    Uri.fromFile(new File(imgPath))));
            return false;
        }
    });
    imageTitle.setText(imgData.getTitle());
    imageDescription.setText(imgData.getDescription());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.tag, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    case R.id.action_save:
        final Intent i = getIntent();
        final String imgPath = i.getExtras().getString("imgPath");
        final File imgFile = new File(imgPath);
        final String imgName = imgFile.getName();
        imgData = new ImageMetadata(this, imgName);

        imgData.setTitle(imageTitle.getText().toString().trim());
        imgData.setDescription(imageDescription.getText().toString().trim());
        File folder = new File(Environment.getExternalStorageDirectory() + File.separator + imageDescription.getText().toString());
        boolean success = true;
        if (!folder.exists()) {
            success = folder.mkdirs();
        }

        if (success) {

        } else {
            // Do something else on failure 
        }

        try {
            File path = Environment.getExternalStoragePublicDirectory(Environment.getExternalStorageDirectory() + File.separator + imgData.getDescription());
            File file = new File(path, imgFile.getName() + ".jpg");
               // the Pictures directory exists? 
                path.mkdirs();
                InputStream is = new FileInputStream(imgPath);
                OutputStream os = new FileOutputStream(file);
                byte[] data = new byte[is.available()];
                is.read(data);
                os.write(data);
                is.close();
                os.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finish();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

1 个答案:

答案 0 :(得分:0)

这看起来不太好:

File path = Environment.getExternalStoragePublicDirectory(Environment.getExternalStorageDire‌​ctory() 
     + File.separator + imgData.getDescription()); 

如果你添加一个toast来查看你生成的路径,你可以解决这个问题:

Toast.makeText(context, "path.getAbsolutePath():\n" + path.getAbsolutePath*(), Toast.LENGTH_LONG).show();