java - 安卓/Java : search the ID of an image inside the device and send it to a client

标签 java android android-image

我看到在我的设备(Htc One S)中,图像存储在这个路径上:storage/sdcard0/DCIM/100MEDIA/name_of_file.jpg

但我确信在其他设备中路径可能不同。好吧,在我的应用程序中,我创建了一个服务器套接字,我可以成功地将图像发送到客户端。正如你在我下面的代码中看到的,我在这种模式下创建了路径>

这对我的设备很好,但对另一个设备可能是错误的。所以现在我有一个问题... 我如何在设备或他的 SD 卡中搜索文件的 ID 或名称以进行发送? 这是我发送图像的代码(它工作正常)。

public class ServerThread extends AndroidApp2 implements Runnable { //dichiaro la classe ServerThread che implementa Runnable

    public void run() {
        try{
            if ( SERVERIP != null){ 
                handler.post(new Runnable(){
                    @Override
                    public void run(){
                        serverStatus.setText("Listening on IP: " + ip_address_conversion + ":" + SERVERPORT);                           
                    }
                }); 
                serverSocket = new ServerSocket(SERVERPORT);
                while (true){
                        Socket client = serverSocket.accept();
                        handler.post(new Runnable(){
                            @Override
                            public void run(){
                                serverStatus.setText("Connected");
                            }
                        });
                        try{
                            handler.post(new Runnable(){
                            @Override
                            public void run(){
                                AlertDialog.Builder connection_alert3 = new AlertDialog.Builder(AndroidApp2.this);
                                connection_alert3.setTitle("Connection Incoming");
                                connection_alert3.setMessage("Do you want accept the incoming connection?");
                                connection_alert3.setCancelable(false);
                                connection_alert3.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
                                    public void onClick(DialogInterface dialog, int id) {
                                        alertValue = true;
                                    }
                                });
                                connection_alert3.setNegativeButton("No", new DialogInterface.OnClickListener(){
                                    public void onClick(DialogInterface dialog, int id){
                                        alertValue = false;
                                    }
                                });
                                AlertDialog alert = connection_alert3.create();
                                alert.show(); 
                            }
                            });
                            if (alertValue == true){
                            File xmlFile = new File(Environment.getExternalStorageDirectory(),"xmlPhotos.xml");
                            int size = (int) xmlFile.length();
                            byte[] bytes = new byte[size];
                            try {
                                BufferedInputStream buf = new BufferedInputStream(new FileInputStream(xmlFile));
                                buf.read(bytes, 0, bytes.length);
                                buf.close();
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            client.getOutputStream().write(bytes, 0, size); 
                            String[] projecton = new String[]{
                                    MediaStore.Images.Media.DATA,
                                };
                            Uri image = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                            Cursor cursor = managedQuery(image, projecton,"", null, "");
                            final ArrayList<String> imagesPath = new ArrayList<String>();
                            if(cursor.moveToFirst()){
                                int dataColumn = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
                              do {
                                imagesPath.add(cursor.getString(dataColumn));
                              } while (cursor.moveToNext());
                            }

                            File photoFile = new File(Environment.getExternalStorageDirectory()+"/"+Environment.DIRECTORY_DCIM+"/100MEDIA","IMAG0026.jpg");
                            int size2 = (int) photoFile.length();
                            System.out.print("int2: "+size2);
                            byte[] bytes2 = new byte[size2];
                            try {
                                BufferedInputStream buf = new BufferedInputStream(new FileInputStream(photoFile)); 
                                buf.read(bytes2, 0, bytes2.length);
                                buf.close();
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            client.getOutputStream().write(bytes2, 0, size2);

                            serverStatus.setText("Finish the transfer");
                            client.close();
                            }
                        } catch (Exception e){
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones."); 
                                }
                            });
                            e.printStackTrace();
                        }
                    }
            } else{
                handler.post(new Runnable(){
                    @Override
                    public void run(){
                        serverStatus.setText("Couldn't detect internet connection.");
                    }
                });
            }
        } catch (Exception e) {
            handler.post(new Runnable(){
                @Override
                public void run(){
                    serverStatus.setText("Error");
                }
            });
            e.printStackTrace();
        }
    }
}

谢谢

最佳答案

您可以通过这种方式获取图库中的图像列表:

String[] projection = new String[]{
        MediaStore.Images.Media.DATA,
};

Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Cursor cur = managedQuery(images,
        projection,
        "",
        null,
        ""
);

final ArrayList<String> imagesPath = new ArrayList<String>();
if (cur.moveToFirst()) {

    int dataColumn = cur.getColumnIndex(
            MediaStore.Images.Media.DATA);
    do {
        imagesPath.add(cur.getString(dataColumn));
    } while (cur.moveToNext());
}

关于java - 安卓/Java : search the ID of an image inside the device and send it to a client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17314843/

相关文章:

android - 在Android中旋转矩形位图

java - 无法在 IntelliJ-IDEA 中访问 JavaFX 类 "WebPage"

java - 通过网站禁用屏幕保护程序/ sleep 模式

android - 在 Android 中裁剪图像(Crop Intent)

android - 手机到调试主机的 TCP 连接

android - Facebook Android SDK 自动登录

Android 保存文件权限被拒绝

android - 如何使用字节数组上传图像

java - 使用 Java API 和 JMF 播放/加载 mp3,错误格式不支持

java - 如何在Project POM中继承External Maven POM