android show image and real path

master
hmkcode 11 years ago
parent ca6c558a43
commit 17af32db1c

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hmkcode.android.image"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

@ -0,0 +1,48 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hmkcode.android.image.MainActivity" >
<TextView
android:id="@+id/txtSDK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btnSelectImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtSDK"
android:text="Select Image"
android:layout_marginBottom="20dp" />
<TextView
android:id="@+id/txtUriPath"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btnSelectImage"
android:text="URI Path:"
android:layout_marginBottom="20dp" />
<TextView
android:id="@+id/txtRealPath"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtUriPath"
android:text="Real Path:"
android:layout_marginBottom="20dp" />
<ImageView
android:id="@+id/imgView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtRealPath"
android:src="@drawable/abc_ab_bottom_solid_light_holo" />
</RelativeLayout>

@ -0,0 +1,106 @@
package com.hmkcode.android.image;
import java.io.File;
import java.io.FileNotFoundException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
TextView txtSDK;
Button btnSelectImage;
TextView txtUriPath,txtRealPath;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get reference to views
txtSDK = (TextView) findViewById(R.id.txtSDK);
btnSelectImage = (Button) findViewById(R.id.btnSelectImage);
txtUriPath = (TextView) findViewById(R.id.txtUriPath);
txtRealPath = (TextView) findViewById(R.id.txtRealPath);
imageView = (ImageView) findViewById(R.id.imgView);
// add click listener to button
btnSelectImage.setOnClickListener(this);
}
@Override
public void onClick(View view) {
// 1. on Upload click call ACTION_GET_CONTENT intent
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// 2. pick image only
intent.setType("image/*");
// 3. start activity
startActivityForResult(intent, 0);
// define onActivityResult to do something with picked image
}
@Override
protected void onActivityResult(int reqCode, int resCode, Intent data) {
if(resCode == Activity.RESULT_OK && data != null){
String realPath;
// SDK < API11
if (Build.VERSION.SDK_INT < 11)
realPath = RealPathUtil.getRealPathFromURI_BelowAPI11(this, data.getData());
// SDK >= 11 && SDK < 19
else if (Build.VERSION.SDK_INT < 19)
realPath = RealPathUtil.getRealPathFromURI_API11to18(this, data.getData());
// SDK > 19 (Android 4.4)
else
realPath = RealPathUtil.getRealPathFromURI_API19(this, data.getData());
setTextViews(Build.VERSION.SDK_INT, data.getData().getPath(),realPath);
}
}
private void setTextViews(int sdk, String uriPath,String realPath){
this.txtSDK.setText("Build.VERSION.SDK_INT: "+sdk);
this.txtUriPath.setText("URI Path: "+uriPath);
this.txtRealPath.setText("Real Path: "+realPath);
Uri uriFromPath = Uri.fromFile(new File(realPath));
// you have two ways to display selected image
// ( 1 ) imageView.setImageURI(uriFromPath);
// ( 2 ) imageView.setImageBitmap(bitmap);
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uriFromPath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
imageView.setImageBitmap(bitmap);
Log.d("HMKCODE", "Build.VERSION.SDK_INT:"+sdk);
Log.d("HMKCODE", "URI Path:"+uriPath);
Log.d("HMKCODE", "Real Path: "+realPath);
}
}

@ -0,0 +1,69 @@
package com.hmkcode.android.image;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.CursorLoader;
import android.database.Cursor;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
public class RealPathUtil {
@SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri){
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(uri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
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;
}
@SuppressLint("NewApi")
public static String getRealPathFromURI_API11to18(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_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);
}
}
Loading…
Cancel
Save