我正在寫一個應用程序,我需要把圖像視圖中的哪個用戶必須通過單擊它來加載圖像。點擊後我可以選擇讓用戶選擇是從電話本身加載存儲的圖像還是從相機中取出新的圖像。Android - 如何把從SD卡或相機膠捲加載圖像的imageview?
這個問題可能是多餘的,但幾乎沒有任何類似的問題/問題在這裏顯示沒有達到我想要做的。
P.s.我正在使用安裝了Eclipse 4.2(JUNO)SDK的Android API15。
這裏是主要活動的代碼片段,給了我一個錯誤:
package test.imgbyte.conerter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.net.Uri;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
public class FindImgPathActivity extends Activity
{
private Uri mImageCaptureUri;
static final int CAMERA_PIC_REQUEST = 1337;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.imgfilepath);
Button camera = (Button) findViewById(R.id.btnLoad);
camera.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST)
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.imgLoaded);
image.setImageBitmap(thumbnail);
String pathToImage = mImageCaptureUri.getPath();
// pathToImage is a path you need.
// If image file is not in there,
// you can save it yourself manually with this code:
File file = new File(pathToImage);
FileOutputStream fOut;
try
{
fOut = new FileOutputStream(file);
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, fOut); // You can choose any format you want
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}
}
錯誤我得到的是這樣的,從logcat的:
11-05 19:23:11.777: E/AndroidRuntime(1206): FATAL EXCEPTION: main
11-05 19:23:11.777: E/AndroidRuntime(1206): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1337, result=-1, data=Intent { act=inline-data (has extras) }} to activity {test.imgbyte.conerter/test.imgbyte.conerter.FindImgPathActivity}: java.lang.NullPointerException
我認爲你可以在這裏參考答案:[http://stackoverflow.com/questions/2507898/how-to-pick-an-image-from-gallery- SD卡換我的應用程序內功能於Android的RQ = 1] [1] [1]:http://stackoverflow.com/questions/2507898/how-to-pick-an -image-from-gallery-sd-card-for-my-app-in-android?rq = 1 –