Мое приложение выдает неожиданное исключение ArrayIndexOutOfBoundsException и даже не сообщает мне номер строки, в которой возникла ошибка. Я попытался использовать журнал для размещения сообщений, чтобы проверить, где код неисправен, и обнаружил, что размер массива равен 5, но в сообщении об ошибке указано java .lang.ArrayIndexOutOfBoundsException: length = 4; index = 4
Я открываю камеру с помощью намерения:
File file = createImageFile();
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
uri = FileProvider.getUriForFile(pdf_viewer.this, BuildConfig.APPLICATION_ID + ".provider", file);
else
uri = Uri.fromFile(file);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(cameraIntent, REQUEST_CODE);
И мой код onActivityResult:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK)
{
try {
new addImageInBack().execute();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Это AsyncMethod, в котором мой код ломается каждый раз:
Log.e("1 - imgRotComp", " OK ");
ArrayList<String> img_paths = send_pdf_images;
File f = new File(Environment.getExternalStorageDirectory(),"/CamScan/.tempFiles");
if(!f.exists())
{
f.mkdirs();
}
Log.e("a", "OK" + img_paths.size());
for(int i = 0 ; i < img_paths.size() ; i++)
{
Log.e("b", "OK "+img_paths.size());
int orientation = 0;
try
{
Log.e("c", "OK");
ExifInterface oldExif = new ExifInterface(send_pdf_images.get(i));
orientation = oldExif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
}
catch (Exception e)
{
e.printStackTrace();
}
Log.e("d", "OK");
File a_file = new File(img_paths.get(i));
Bitmap a_bitmap = BitmapFactory.decodeFile(a_file.getAbsolutePath());
float a_op_width = a_bitmap.getWidth();
float a_op_height = a_bitmap.getHeight();
if (a_op_width < 1500 && orientation == 0)
continue;
Log.e("e", "OK");
if(orientation == 6 || orientation == 3 || orientation == 8)
{
Matrix rotateMatrix = new Matrix();
if (orientation == 6)
rotateMatrix.postRotate(90);
else if (orientation == 3)
rotateMatrix.postRotate(180);
else if (orientation == 8)
rotateMatrix.postRotate(270);
Bitmap rotatedBitmap = Bitmap.createBitmap(a_bitmap, 0, 0, a_bitmap.getWidth(), a_bitmap.getHeight(), rotateMatrix, false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
try
{
Calendar c = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("d-M-yy-H-m-s", Locale.getDefault());
String dateTime = dateFormat.format(c.getTime());
dateTime = dateTime.replace("-", "");
File temp_file = new File(f.toString() + "/", dateTime + ".jpg");
temp_file.createNewFile();
FileOutputStream fo = new FileOutputStream(temp_file);
fo.write(bytes.toByteArray());
fo.close();
Uri uri = Uri.fromFile(temp_file);
String realPath = getRealPathFromURI(uri);
Log.e("l1","OK");
send_pdf_images.remove(i);
send_pdf_images.add(i, realPath);
Log.e("l2","OK");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Странно то, что мой код ломается каждый раз, когда я нажимаю 4-е изображение до этого, он работает отлично. t разрывается между строкой Log.e ("d", "OK"); и Log.e ("e", "OK"); (только во время клика 4-го изображения) .
Это мой полный журнал:
E/a: OK5
E/b: OK 5
E/c: OK
E/d: OK
E/b: OK 5
E/c: OK
E/d: OK
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.camscan, PID: 27178
java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:6917)
at android.widget.GridView.layoutChildren(GridView.java:1268)
at android.widget.AbsListView.onLayout(AbsListView.java:2165)
at android.view.View.layout(View.java:20680)
at android.view.ViewGroup.layout(ViewGroup.java:6197)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
at android.view.View.layout(View.java:20680)
at android.view.ViewGroup.layout(ViewGroup.java:6197)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20680)
at android.view.ViewGroup.layout(ViewGroup.java:6197)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20680)
at android.view.ViewGroup.layout(ViewGroup.java:6197)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20680)
at android.view.ViewGroup.layout(ViewGroup.java:6197)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20680)
at android.view.ViewGroup.layout(ViewGroup.java:6197)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:788)
at android.view.View.layout(View.java:20680)
at android.view.ViewGroup.layout(ViewGroup.java:6197)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2830)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2357)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1493)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7283)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:696)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6912)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)
I/Process: Sending signal. PID: 27178 SIG: 9
Process 27178 terminated.
EDIT: PDFViewAdapter. java
package com.example.camscan;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import java.util.ArrayList;
public class PDFViewAdapter extends ArrayAdapter<String> {
Context context;
ViewHolder viewHolder;
ArrayList<String> pdf_images = new ArrayList<>();
public PDFViewAdapter(Context context, ArrayList<String> pdf_images) {
super(context, R.layout.pdf_image_view, pdf_images);
this.pdf_images = pdf_images;
this.context = context;
}
@Override
public int getCount() {
return pdf_images.size();
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return pdf_images.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.pdf_image_view, parent, false);
viewHolder.iv_image = (ImageView) convertView.findViewById(R.id.pdfiv);
viewHolder.tv = (TextView)convertView.findViewById(R.id.pdftv);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tv.setTag(position);
Glide.with(context).load(pdf_images.get(position))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(viewHolder.iv_image);
viewHolder.tv.setText(""+(position + 1));
return convertView;
}
private static class ViewHolder {
ImageView iv_image;
TextView tv;
}
}
Что может быть причиной этого? Спасибо