Java 安卓没有';t显示内部存储器上存在的任何图像
我试图访问android内部存储器上的图像,但它没有显示任何图像。图像存在,但不显示它。它仅显示外部存储器映像。意向被传递到ImageDirectory活动(此处未显示该活动以减少代码大小),该活动包含用户想要访问的额外选定内存。它显示在我的代码中 下面是我的代码 我有以下活动:Java 安卓没有';t显示内部存储器上存在的任何图像,java,android,storage,Java,Android,Storage,我试图访问android内部存储器上的图像,但它没有显示任何图像。图像存在,但不显示它。它仅显示外部存储器映像。意向被传递到ImageDirectory活动(此处未显示该活动以减少代码大小),该活动包含用户想要访问的额外选定内存。它显示在我的代码中 下面是我的代码 我有以下活动: package com.projects.timely.gallery; import android.content.Intent; import android.os.Bundle;
package com.projects.timely.gallery;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.projects.timely.R;
@SuppressWarnings("ConstantConditions")
public class StorageViewer extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_storage_view);
setSupportActionBar(findViewById(R.id.toolbar));
getSupportActionBar().setTitle("Select Storage");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ViewGroup v_internalStorage = findViewById(R.id.internal_storage);
v_internalStorage.setOnClickListener(this);
ViewGroup v_externalStorage = findViewById(R.id.external_storage);
v_externalStorage.setOnClickListener(this);
boolean ext_str_mounted
= Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
v_externalStorage.setVisibility(ext_str_mounted ? View.VISIBLE : View.GONE);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.internal_storage) {
startActivity(new Intent(this, ImageDirectory.class)
.putExtra(ImageDirectory.STORAGE_ACCESS_ROOT, ImageDirectory.INTERNAL));
} else {
startActivity(new Intent(this, ImageDirectory.class)
.putExtra(ImageDirectory.STORAGE_ACCESS_ROOT, ImageDirectory.EXTERNAL));
}
finish();
}
@Override
public boolean onSupportNavigateUp() {
super.onBackPressed();
return true;
}
}
这将触发此代码。此代码在活动中执行。它位于名为ImageDirectory.java的文件中
@Override
@SuppressLint("InlinedApi")
public void run() {
String root_extra = getIntent().getStringExtra(STORAGE_ACCESS_ROOT);
Uri storageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
if (root_extra != null) {
storageUri = root_extra.equals(EXTERNAL) ? MediaStore.Images.Media.EXTERNAL_CONTENT_URI
: MediaStore.Images.Media.INTERNAL_CONTENT_URI;
}
String[] projection = {
MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.SIZE,
MediaStore.Images.Media.DISPLAY_NAME};
Cursor imgCursor = getApplicationContext()
.getContentResolver()
.query(storageUri, projection, null, null, null);
int bucketId = imgCursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
int imgSize = imgCursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE);
int name = imgCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
int bucketName
= imgCursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
List<String> dirName = new ArrayList<>();
while (imgCursor.moveToNext()) {
long id = imgCursor.getLong(bucketId);
int size = imgCursor.getInt(imgSize);
String fileName = imgCursor.getString(name);
String folderName = imgCursor.getString(bucketName);
// Updated this part, replaced with storageUri
Uri contentUri
= ContentUris.withAppendedId(storageUri, id);
Image currentImage = new Image(contentUri, size, fileName, folderName);
int directoryIndex = linearSearch(dirName, folderName);
// if search result (directoryIndex) passes this test, then it means that there is
// no such directory in list of directory names
if (directoryIndex < 0) {
imageDirectoryList.add(new ArrayList<>());
dirName.add(folderName);
directoryIndex = linearSearch(dirName, folderName);
if (directoryIndex >= 0)
imageDirectoryList.get(directoryIndex).add(currentImage);
} else {
imageDirectoryList.get(directoryIndex).add(currentImage);
}
}
imgCursor.close();
runOnUiThread(() -> {
imageAdapter.notifyDataSetChanged();
doViewUpdate();
});
}
@覆盖
@SuppressLint(“InlinedApi”)
公开募捐{
String root\u extra=getIntent().getStringExtra(存储\u访问\u根);
Uri storageUri=MediaStore.Images.Media.EXTERNAL\u CONTENT\u Uri;
if(root_extra!=null){
storageUri=root\u extra.equals(外部)?MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI
:MediaStore.Images.Media.INTERNAL_CONTENT_URI;
}
字符串[]投影={
MediaStore.Images.Media.\u ID,
MediaStore.Images.Media.BUCKET\u显示\u名称,
MediaStore.Images.Media.SIZE,
MediaStore.Images.Media.DISPLAY\u NAME};
游标imgCursor=getApplicationContext()
.getContentResolver()
.query(存储URI、投影、null、null、null);
int bucketId=imgCursor.getColumnIndexOrThrow(MediaStore.Images.Media.\u ID);
int imgSize=imgCursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE);
int name=imgCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_name);
整数bucketName
=imgCursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET\u DISPLAY\u NAME);
List dirName=new ArrayList();
while(imgCursor.moveToNext()){
long id=imgCursor.getLong(bucketId);
int size=imgCursor.getInt(imgSize);
字符串文件名=imgCursor.getString(名称);
String folderName=imgCursor.getString(bucketName);
//更新了此部分,替换为storageUri
Uri内容Uri
=ContentUris.withAppendedId(存储URI,id);
Image currentImage=新图像(contentUri、大小、文件名、文件夹名);
int directoryIndex=linearSearch(dirName,folderName);
//如果搜索结果(directoryIndex)通过此测试,则表示存在
//目录名列表中没有这样的目录
如果(目录索引<0){
添加(新的ArrayList());
dirName.add(folderName);
directoryIndex=linearSearch(dirName,folderName);
如果(目录索引>=0)
获取(目录索引).add(当前图像);
}否则{
获取(目录索引).add(当前图像);
}
}
imgCursor.close();
runOnUiThread(()->{
imageAdapter.notifyDataSetChanged();
doViewUpdate();
});
}
也许可以共享堆栈跟踪?好的,我会尝试这样做。这很尴尬,但我的USB端口有问题,因此可能很难获得堆栈跟踪,因为我必须构建apk并直接在设备上安装apk,而不使用Android studio。所以这可能需要一段时间。@HenryTwist我现在在代码中发现了一个bug,并对其进行了更新。我将更新问题描述