我正在开发使用相机的应用程序,该相机允许用户捕获照片并显示到GridView中。它工作正常,直到它将图片保存到App Specific文件夹但是,当它是时候在GridView中显示它们时,突然它崩溃并给出例外。我正在附加编码内容和Logcat。然而,我试图弄清楚,但我不能。请帮助!!
这是相机类:
public class OwnerAddPhoto extends Activity implements OnClickListener {
private Button btnAdd, btnUpload;
private GridView photoGrid;
ArrayList<String> filePath;
File[] listOfFile;
ImageAdapter getSDImgs;
private static final int PIC_FROM_CAMERA = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.owner_photoupload);
btnAdd = (Button) findViewById(R.id.btn_addphoto);
btnUpload = (Button) findViewById(R.id.btn_uploadphoto);
btnAdd.setOnClickListener(this);
btnUpload.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_addphoto:
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
String root = Environment.getExternalStorageDirectory()
.toString();
File DirHomey = new File(root, "/Homey");
DirHomey.mkdirs();
if (DirHomey.isDirectory()) {
Log.d("Directory Available.", DirHomey.toString());
Intent cameraIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
Random rdm = new Random();
int n = 1000;
n = rdm.nextInt();
String imgName = "IMG_" + n + ".jpg";
File imgFile = new File(DirHomey, imgName);
if (imgFile.exists()) {
imgFile.delete();
}
Log.d("Image File Created.", imgFile.toString());
Uri extraFilePath = Uri.fromFile(imgFile);
Log.d("IMGPath", extraFilePath.toString());
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
extraFilePath);
startActivityForResult(cameraIntent, PIC_FROM_CAMERA);
}
}
break;
case R.id.btn_uploadphoto:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_CANCELED && requestCode == PIC_FROM_CAMERA
&& data != null) {
getSDCARDImagePaths();
getSDImgs = new ImageAdapter(this, filePath);
photoGrid.setAdapter(getSDImgs);
} else {
Toast.makeText(this, "Ops.. Error Occurred! Try Again.",
Toast.LENGTH_SHORT).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
private void getSDCARDImagePaths() {
filePath = new ArrayList<String>();
File DirPath = new File(Environment.getExternalStorageDirectory(),
"Homey");
if (DirPath.isDirectory()) {
listOfFile = DirPath.listFiles();
for (int i = 0; i <= listOfFile.length; i++) {
filePath.add(listOfFile[i].getAbsolutePath());
}
}
}
}
这是LogCat屏幕:
03-18 04:00:27.386: W/dalvikvm(1240): threadid=1: thread exiting with uncaught exception (group=0xb3aeeb90)
03-18 04:00:27.406: E/AndroidRuntime(1240): FATAL EXCEPTION: main
03-18 04:00:27.406: E/AndroidRuntime(1240): Process: com.example.homeyapp, PID: 1240
03-18 04:00:27.406: E/AndroidRuntime(1240): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.example.homeyapp/com.example.homeyapp.OwnerAddPhoto}: java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
03-18 04:00:27.406: E/AndroidRuntime(1240): at android.app.ActivityThread.deliverResults(ActivityThread.java:3346)
03-18 04:00:27.406: E/AndroidRuntime(1240): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3389)
03-18 04:00:27.406: E/AndroidRuntime(1240): at android.app.ActivityThread.access$1200(ActivityThread.java:135)
03-18 04:00:27.406: E/AndroidRuntime(1240): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1445)
03-18 04:00:27.406: E/AndroidRuntime(1240): at android.os.Handler.dispatchMessage(Handler.java:102)
03-18 04:00:27.406: E/AndroidRuntime(1240): at android.os.Looper.loop(Looper.java:137)
03-18 04:00:27.406: E/AndroidRuntime(1240): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-18 04:00:27.406: E/AndroidRuntime(1240): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 04:00:27.406: E/AndroidRuntime(1240): at java.lang.reflect.Method.invoke(Method.java:515)
03-18 04:00:27.406: E/AndroidRuntime(1240): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-18 04:00:27.406: E/AndroidRuntime(1240): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-18 04:00:27.406: E/AndroidRuntime(1240): at dalvik.system.NativeStart.main(Native Method)
03-18 04:00:27.406: E/AndroidRuntime(1240): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
03-18 04:00:27.406: E/AndroidRuntime(1240): at com.example.homeyapp.OwnerAddPhoto.getSDCARDImagePaths(OwnerAddPhoto.java:117)
03-18 04:00:27.406: E/AndroidRuntime(1240): at com.example.homeyapp.OwnerAddPhoto.onActivityResult(OwnerAddPhoto.java:96)
03-18 04:00:27.406: E/AndroidRuntime(1240): at android.app.Activity.dispatchActivityResult(Activity.java:5435)
03-18 04:00:27.406: E/AndroidRuntime(1240): at android.app.ActivityThread.deliverResults(ActivityThread.java:3342)
03-18 04:00:27.406: E/AndroidRuntime(1240): ... 11 more
答案 0 :(得分:1)
从=
条件中删除i <= listOfFile.length;
运算符,如下所示......
for (int i = 0; i < listOfFile.length; i++) {
filePath.add(listOfFile[i].getAbsolutePath());
}
您也可以使用i <= listOfFile.length-1;
条件解决问题,如下所示......
for (int i = 0; i <= listOfFile.length-1; i++) {
filePath.add(listOfFile[i].getAbsolutePath());
}
因此,您更新的getSDCARDImagePaths()
方法将是......
private void getSDCARDImagePaths() {
filePath = new ArrayList<String>();
File DirPath = new File(Environment.getExternalStorageDirectory(),
"Homey");
if (DirPath.isDirectory()) {
listOfFile = DirPath.listFiles();
for (int i = 0; i < listOfFile.length; i++) {
filePath.add(listOfFile[i].getAbsolutePath());
}
}
}
答案 1 :(得分:0)
试试这个
private void getSDCARDImagePaths() {
filePath = new ArrayList<String>();
File DirPath = new File(Environment.getExternalStorageDirectory(),
"Homey");
if (DirPath.isDirectory()) {
listOfFile = DirPath.listFiles();
for (int i = 0; i < listOfFile.length; i++) {
filePath.add(listOfFile[i].getAbsolutePath());
}
}
}
答案 2 :(得分:0)
for (int i = 0; i <= listOfFile.length; i++) {
filePath.add(listOfFile[i].getAbsolutePath());
}
应该是
for (int i = 0; i < listOfFile.length; i++) {
filePath.add(listOfFile[i].getAbsolutePath());
}
因为你的数组是从0到长度 - 1