1. 이미지 가져오기
2. URI -> path 변환
3. 안드로이드에서 파일 복사하기
package com.example.changnam.kcntest;
import android.content.ContentUris;import android.content.Context;import android.content.Intent;import android.database.Cursor;import android.graphics.drawable.Drawable;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.DocumentsContract;import android.provider.MediaStore;import android.support.design.widget.FloatingActionButton;import android.support.design.widget.Snackbar;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.util.Log;import android.view.View;import android.view.Menu;import android.view.MenuItem;import android.widget.ImageView;
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
public static final int TAKE_FROM_GALLERY = 100;
String mStrDestFolder = Environment.getExternalStorageDirectory().getAbsolutePath()+"/Download/test/";
ImageView imageview;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } });
imageview = (ImageView)findViewById(R.id.imageview);
takeImage();
// testCopy(); }
private void takeImage(){ Intent intent = new Intent(); intent.setAction(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivityForResult(intent, TAKE_FROM_GALLERY); }
/** * 파일 복사 * @param strSrc * @param save_file * @return */ private boolean copyFile(String strSrc , String save_file){ File file = new File(strSrc);
boolean result; if(file!=null&&file.exists()){
try {
FileInputStream fis = new FileInputStream(file); FileOutputStream newfos = new FileOutputStream(save_file); int readcount=0; byte[] buffer = new byte[1024];
while((readcount = fis.read(buffer,0,1024))!= -1){ newfos.write(buffer,0,readcount); } newfos.close(); fis.close(); } catch (Exception e) { e.printStackTrace(); } result = true; }else{ result = false; } return result; }
public String getPathFromUri(Uri uri){
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0];
if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; }
// TODO handle non-primary volumes } // DownloadsProvider else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris.withAppendedId( Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(this, contentUri, null, null); } // MediaProvider else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0];
Uri contentUri = null; if ("image".equals(type)) { contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; }
final String selection = "_id=?"; final String[] selectionArgs = new String[] { split[1] };
return getDataColumn(this, contentUri, selection, selectionArgs); }
return null;
}
/** * @param uri The Uri to check. * @return Whether the Uri authority is ExternalStorageProvider. */ public static boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri.getAuthority()); }
/** * @param uri The Uri to check. * @return Whether the Uri authority is DownloadsProvider. */ public static boolean isDownloadsDocument(Uri uri) { return "com.android.providers.downloads.documents".equals(uri.getAuthority()); }
/** * @param uri The Uri to check. * @return Whether the Uri authority is MediaProvider. */ public static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); }
/** * Get the value of the data column for this Uri. This is useful for * MediaStore Uris, and other file-based ContentProviders. * * @param context The context. * @param uri The Uri to query. * @param selection (Optional) Filter used in the query. * @param selectionArgs (Optional) Selection arguments used in the query. * @return The value of the _data column, which is typically a file path. */ public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null; final String column = "_data"; final String[] projection = { column };
try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int column_index = cursor.getColumnIndexOrThrow(column); return cursor.getString(column_index); } } finally { if (cursor != null) cursor.close(); }
return null; }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == RESULT_OK){ switch (requestCode){
case TAKE_FROM_GALLERY: Uri imageUri = data.getData(); Log.d("5sd6sd","imageeUri : "+imageUri);
String strSrcpath = getPathFromUri(imageUri); String destFileName = strSrcpath.substring(strSrcpath.lastIndexOf("/")); String strDestPath = mStrDestFolder+destFileName;
Log.d("5sd6sd", "destFileName : " + destFileName); Log.d("5sd6sd","strSrcpath :"+strSrcpath); Log.d("5sd6sd", "strDestPath :" + strDestPath);
copyFile(strSrcpath, strDestPath);
imageview.setImageDrawable(Drawable.createFromPath(strDestPath));
break;
} } }
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; }
@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId();
//noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; }
return super.onOptionsItemSelected(item); }
}
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> |
<TextView android:id="@+id/textView" android:text="Hello World!" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/textView" android:background="#00ff00" /> |
|